[RESOLVED]easy one - calling macro from another
I'm writing a macro which goes through each shape, header and footer in a Word document individually and runs an external macro on it. This external macro was not written by me and I've no access to the code.
So I have the macro loop through all the objects in question and call the macro with Run, but the whole thing seems to stop running after the Run statement, and the macro appears to run on the whole document, excluding the headers and footers. Should I be using something else rather than Run?
VB Code:
Sub CleanWholeDocument()
Dim sShape As Shape
Dim fNote As Footnote
Dim HeadFoot As HeaderFooter
Dim sSection As Section
For Each sSection In ActiveDocument.Sections
For Each HeadFoot In sSection.Headers
HeadFoot.Range.Select
Application.Run MacroName:="tw4winClean.Main"
Next HeadFoot
Next sSection
For Each fNote In ActiveDocument.Footnotes
fNote.Range.Select
Application.Run MacroName:="tw4winClean.Main"
Next fNote
For Each sShape In ActiveDocument.Shapes
If sShape.TextFrame.HasText Then
sShape.TextFrame.TextRange.Select
Application.Run MacroName:="tw4winClean.Main"
End If
Next sShape
Application.Run MacroName:="tw4winClean.Main"
End Sub
Re: easy one - calling macro from another
have you tried to run the macro 1 header at a time?
try pressing Ctrl Break while th macro is running to see were it stops and if it is in an continuous loop
pete
2 Attachment(s)
Re: easy one - calling macro from another
I put a breakpoint on HeadFoot.Range.Select and hit F5. I expected it to open the headers and footers as in footer.jpg but it opens the window in header.jpg
This may be the root of the problem? I thought the macro was ending after the first call to the external macro, as when I step through it with F8 it finishes immediately after the first call to it. But when I put a breakpoint in the third loop and hit F5 it stops there, so it must actually be flying through the rest of the code.
Re: easy one - calling macro from another
might pay to put debug statements after each section to see how far it has goes before it gets stuck
pete
Re: easy one - calling macro from another
I think ur problem is that the macro is calling 2 times. Once inside theloop and once outside.
Why do u need the last
Code:
Application.Run MacroName:="tw4winClean.Main"
before End Sub when u r explicitly checking them in the loops. Try commenting that line and c if it works.
Pradeep
Re: easy one - calling macro from another
I think I had it wrong in the beginning, it's running the whole macro (just to quickly for F8), but it's not handling the headers/footers the way I expected it to.
I'll see if there's not another object which relates to the page headings the way I want it to.
Re: easy one - calling macro from another
Quote:
Originally Posted by Pradeep1210
I think ur problem is that the macro is calling 2 times. Once inside theloop and once outside.
Why do u need the last
Code:
Application.Run MacroName:="tw4winClean.Main"
before End Sub when u r explicitly checking them in the loops. Try commenting that line and c if it works.
Pradeep
The last call to tw4winClean.Main is supposed to run the external macro on the rest of the document, i.e., the parts outside shapes, headers and footers.
I changed my approach to the headers and footers, using windows and panes instead, and now I have the exact opposite problem - the headers/footers are handled, but the rest of the document no longer is!
VB Code:
Sub CleanWholeDocument()
Dim sShape As Shape
Dim fNote As Footnote
Dim wWindow As Window
Dim pPane As Pane
For Each wWindow In ActiveDocument.Windows
For Each pPane In wWindow.Panes
pPane.View.Type = wdPrintView
pPane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Application.Run MacroName:="tw4winClean.Main"
pPane.View.SeekView = wdSeekCurrentPageFooter
Selection.WholeStory
Application.Run MacroName:="tw4winClean.Main"
Next pPane
Next wWindow
For Each fNote In ActiveDocument.Footnotes
fNote.Range.Select
Application.Run MacroName:="tw4winClean.Main"
Next fNote
For Each sShape In ActiveDocument.Shapes
If sShape.TextFrame.HasText Then
sShape.TextFrame.TextRange.Select
Application.Run MacroName:="tw4winClean.Main"
End If
Next sShape
Selection.WholeStory
Application.Run MacroName:="tw4winClean.Main"
End Sub
I think I'm just not reselecting the document properly somewhere.
[edit] moving the last line to the top of the macro, so the main body of the document gets processed first does the trick[/edit]
Thanks to everyone