Results 1 to 6 of 6
  1. #1
    Star Lounger
    Join Date
    Jul 2008
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How do I run a macro from the top of the document to the end of the document? In my macros, normally I process a document paragraph to paragraph. As an example, in the past if I was going to do a replace I would count the number of replaces to be done, do a For Next loop around the macro and run the macro using the For Next. Is there a better way?

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi HowdeeDoodee,

    It all depends on what sort of processing you're doing.

    For example, if you were using a Find/Replace routine, you might use:
    With ActiveDocument.Content.Find
    ...
    End With

    Alternatively, if you wanted to test cells in tables, you might use code like that I've just posted in:
    http://lounge.windowssecrets.com/ind...owtopic=779723
    You could use similar code for processing a collection of paragraphs.

    In other cases, you might need to loop through or process particular StoryRanges Shape/InlineShape collections, etc. Just because something is associated with paragraphs (eg InlineShapes) doesn't mean that iterating through all paragraphs is the way to go. It's very much a case of 'horses for courses'.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Star Lounger
    Join Date
    Jul 2008
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply macropod.

    Here is an example. As you can see, in this example, I have an endless loop because I am not destroying the Find criteria. I run macros like this one. As a general strategy, how do I run Find loops without destroying the Find criteria in the document.

    Code:
    Sub MakeBookNameAndFollowingTextBold()
    '
    ' MakeBookNameAndFollowingTextBold Macro
    ' Macro recorded 11/21/2010 by Patrick Kelly
    'assigned to Ctrl+5
    
    With Selection
        ' To top of document
        .HomeKey Unit:=wdStory
        ' Set Find parameters
        With .Find
            .ClearFormatting
            .Text = ""
            .Font.Bold = True   ' Search for runs of bold
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
    
    Do
     
     If .Find.Execute = False Then Exit Do
    
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "^t^t^t^t^t"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            
        End With
        Selection.Find.Execute
        Selection.MoveRight Unit:=wdCharacter, Count:=17, Extend:=wdExtend
        Selection.Font.Bold = wdToggle
        Selection.MoveRight Unit:=wdCharacter, Count:=1
          
    Loop
        ' Clear Find parameters
       With .Find
           .ClearFormatting
           .Format = False
        End With
    
    
    End With
    
    End Sub

  4. #4
    Star Lounger
    Join Date
    Jul 2008
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found this suggestion somewhere else and thought this answer might help others.

    A Google Groups search found this clip from an answer to a post
    in 2003.
    -------------------------------------------
    This example macro shows the general outline of how to search repeatedly
    until nothing more is found. [Note the difference in criteria -- not "until
    end of document", but "until no more finds".]
    Code:
    Sub FindAndFix()
       Dim MyRange As Range
    
       Set MyRange = ActiveDocument.Range
    
       With MyRange.Find
           ' set parameters as needed --
           ' beware of "sticky" values from
           ' previous searches
           .ClearFormatting
           .Text = "target"
           .Format = False
           .Forward = True        ' important
           .Wrap = wdFindStop    'important
           .MatchWildcards = False
    
           ' the .Execute method returns True when it
           ' finds something, False when unsuccessful
           Do While .Execute
               ' when .Execute is successful,
               ' MyRange now includes only the found text
    
               ' do something to MyRange here
               MyRange.Sentences(1).Case = wdTitleWord
    
               ' start next search after current find
               ' to avoid finding the same one again
               MyRange.Collapse wdCollapseEnd
           Loop
       End With
    End Sub

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Something bad happened to my macro.

    I think you have hit on the answer: there can only be one selection at a time, but you can use any number of ranges you like, so when you need to search within a search result, you should use ranges.

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi HowdeeDoodee,

    I've recently posted a number of Find/Replace routines, for which you might be interested in the algorithms, at:
    http://www.techsupportforum.com/micr...elp-macro.html
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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