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.
Be aware when you do this sort of thing that you are setting up something that runs automatically - all the time, in any document that is active.
You might want to give some thought to having some sort of "switch" that you can use to set whether you want this code to be executing automatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Public blnMyPageNumbers_active As Boolean 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 Sub set_MyPageNumbers_active() blnMyPageNumbers_active = True End Sub Sub set_MyPageNumbers_inactive() blnMyPageNumbers_active = False End Sub
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) If blnMyPageNumbers_active Then update_my_page_numbers End If End Sub Private Sub CurDoc_PageDelete(ByVal Count As Long) If blnMyPageNumbers_active Then update_my_page_numbers End If End Sub
thanks. how do you post code this proper way?
also, here is the 'thismacrostorage' code and the module that is not running on page add or delete.
Dim WithEvents CurDoc As DocumentPrivate Sub GlobalMacroStorage_WindowActivate(ByVal Doc As Document, ByVal Window As Window) Set CurDoc = DocEnd SubPrivate Sub GlobalMacroStorage_WindowDeactivate(ByVal Doc As Document, ByVal Window As Window) Set CurDoc = NothingEnd SubPrivate Sub CurDoc_PageCreate(ByVal Page As Page) updateEnd SubPrivate Sub CurDoc_PageDelete(ByVal Count As Long) updateEnd Sub
Sub update()Dim pageThis As PageDim s As Shape For Each pageThis In ActiveDocument.Pages Set sPageNumThis = pageThis.Shapes.FindShape("pnumtot") If Not sPageNumThis Is Nothing Then sPageNumThis.Text.Story = CStr(ActiveDocument.Pages.Count) End If Next pageThisEnd Sub
This may not fix your problem, but I fixed an error in the code I posted originally.
I declare "s" as a shape, and use that to work with the text shape.
I don't use any "sPageNumThis".
If one uses "Option Explicit" at the top of the module - and I usually do - then it will catch those sorts of mistakes.
To make the code post in a pretty way, I copy the text in the VBA editor, then use the tool on the hilite.me web site to produce HTML (with the language set to VB.net, which is usually close enough to be useful).
To put that HTML into a post, use Tools>Source Code in the editor here on the forum.
ggut said:also, here is the 'thismacrostorage' code and the module that is not running on page add or delete
If I
...then it works as expected for me when I create or delete pages in a document.
When things don't work as expected, you may need to do some debugging. That might include adding break points, using Tools>Step Into, using the Locals window, etc.
I don't know if you are already familiar with John's GDG Macros VBA Lessons, but there is a lot of good stuff there.