macro - find object text and replace

hi.  I'm trying to put together a macro that will search for an object with the label PNUMTOT and replace it with the total number of pages.  any help is 'preesh.

Parents Reply
  • See this recent thread: macro request - carry out a function when a page is added or deleted.

    The PageCreate and PageDelete events are Document events.

    In the example from Alex, he declares CurDoc as a Document, and declares it in such a way that events are available. He uses GlobalMacroStorage events (WindowActivate and WindowDeactivate) to set CurDoc to the active document (or to Nothing, if there is no active document).

    When CurDoc is something, then the PageCreate and PageDelete events associated with it can be used to do stuff.

    You could have this in the ThisMacroStorage part of a project:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    Dim WithEvents CurDoc As Document
    
    Private Sub GlobalMacroStorage_WindowActivate(ByVal Doc As Document, ByVal Window As Window)
        Set CurDoc = Doc
    End Sub
    
    Private Sub GlobalMacroStorage_WindowDeactivate(ByVal Doc As Document, ByVal Window As Window)
        Set CurDoc = Nothing
    End Sub
    
    Private Sub CurDoc_PageCreate(ByVal Page As Page)
        update_my_page_numbers
    End Sub
    
    Private Sub CurDoc_PageDelete(ByVal Count As Long)
        update_my_page_numbers
    End Sub
    

    and this in another module in that project:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    Sub update_my_page_numbers()
    Dim pageThis As Page
    Dim s As Shape
    
        For Each pageThis In ActiveDocument.Pages
            Set s = pageThis.Shapes.FindShape("PAGENUMBERS")
            If Not s Is Nothing Then
                s.Text.Story = "Page " & CStr(pageThis.Index) & " of " & CStr(ActiveDocument.Pages.Count)
            End If
        Next pageThis
    End Sub
    
Children