Results 1 to 7 of 7

Thread: [RESOLVED]easy one - calling macro from another

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Resolved [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:
    1. Sub CleanWholeDocument()
    2.  
    3. Dim sShape As Shape
    4. Dim fNote As Footnote
    5. Dim HeadFoot As HeaderFooter
    6. Dim sSection As Section
    7.  
    8. For Each sSection In ActiveDocument.Sections
    9.     For Each HeadFoot In sSection.Headers
    10.         HeadFoot.Range.Select
    11.         Application.Run MacroName:="tw4winClean.Main"
    12.     Next HeadFoot
    13. Next sSection
    14.  
    15. For Each fNote In ActiveDocument.Footnotes
    16.     fNote.Range.Select
    17.     Application.Run MacroName:="tw4winClean.Main"
    18. Next fNote
    19.  
    20. For Each sShape In ActiveDocument.Shapes
    21.     If sShape.TextFrame.HasText Then
    22.         sShape.TextFrame.TextRange.Select
    23.         Application.Run MacroName:="tw4winClean.Main"
    24.     End If
    25. Next sShape
    26.          
    27. Application.Run MacroName:="tw4winClean.Main"
    28.  
    29. End Sub
    Last edited by pickarooney; May 19th, 2005 at 06:01 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    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.
    Attached Images Attached Images   

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    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.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    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:
    1. Sub CleanWholeDocument()
    2.  
    3. Dim sShape As Shape
    4. Dim fNote As Footnote
    5. Dim wWindow As Window
    6. Dim pPane As Pane
    7.  
    8. For Each wWindow In ActiveDocument.Windows
    9.     For Each pPane In wWindow.Panes
    10.         pPane.View.Type = wdPrintView
    11.         pPane.View.SeekView = wdSeekCurrentPageHeader
    12.         Selection.WholeStory
    13.         Application.Run MacroName:="tw4winClean.Main"
    14.         pPane.View.SeekView = wdSeekCurrentPageFooter
    15.         Selection.WholeStory
    16.         Application.Run MacroName:="tw4winClean.Main"
    17.     Next pPane
    18. Next wWindow
    19.  
    20. For Each fNote In ActiveDocument.Footnotes
    21.     fNote.Range.Select
    22.     Application.Run MacroName:="tw4winClean.Main"
    23. Next fNote
    24.  
    25. For Each sShape In ActiveDocument.Shapes
    26.     If sShape.TextFrame.HasText Then
    27.         sShape.TextFrame.TextRange.Select
    28.         Application.Run MacroName:="tw4winClean.Main"
    29.     End If
    30. Next sShape
    31.          
    32. Selection.WholeStory
    33. Application.Run MacroName:="tw4winClean.Main"
    34.  
    35. 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
    Last edited by pickarooney; May 19th, 2005 at 05:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width