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.
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:
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
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.
Yes, I have bought a few of GDG macros. This one is very minimal and catered for a specific need but I appreciate the tip as well as all the help. I got it to work finally. YAY!
The link I posted is to a set of lessons from John on using VBA. It's a great place for someone to start if they want to make the jump from recorded macros in CorelDRAW to something more substantial.
There's a lot of good stuff in there about working with the CorelDRAW API, VBA in general, and the editing/debugging environment.
grogo said: I got it to work finally. YAY!
Great news!
While we're here, I'll promote my blog - Eskimo's Macros and More.