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.
or rather I just need to replace the selected text to the total number of pages. here is a messy attempt that does not work.
Sub pnumtots() Dim s As Shape, sr As ShapeRange Dim q$ ActiveDocument.BeginCommandGroup q = "(@type = 'text:artistic' or @type = 'text:paragraph')" Set sr = ActiveSelection.Shapes.FindShapes(Query:=q)cur = ActivePage.Index If sr.Count = 0 Then Exit Sub numPages = ActiveDocument.Pages.Count For Each p In ActiveDocument.Pages p.Activate For Each s In sr s.Text.Story.Characters.All = (numPages) Next s 'Optimization = True ActiveDocument.EndCommandGroup ActiveDocument.Pages(cur).ActivateEnd Sub
Here's an example where it walks through all the pages in the document.
On each page, it looks for only one shape named, "PAGENUMBERS".
It assumes that shape is text, and sets it to be, for example, "Page 1 of 7".
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
Even if that's not exactly what you want to do, it might provide some clues.
wonderful. Thank you so much!
I have this in my globalmacro storage section of the macro but something is wrong. I need it to run the macro Module1.Update upon page add or delete.
Private Sub GlobalMacroStorage_PageCreate() Module1.updateEnd SubPrivate Sub GlobalMacroStorage_PageDelete() Module1.updateEnd Sub
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: