Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Missouri, USA
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple question (Word 2000/VBA)

    Hopefully this is a really simple question -- how do I code for something not found?

    The scenario: I've got a lot of seperate documents that I need to strip certain styles out of. So far my code is:

    Do

    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Sub-section")
    Selection.Find.ParagraphFormat.Borders.Shadow = False
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    End With
    Selection.Find.Execute

    If {{selection found = true}} Then
    Selection.Delete Unit:=wdCharacter, Count:=1
    Else
    End If

    Loop Until {{selection found = false}}

    Since there may be none to many of the particular style in any given document, I need to have this function loop BUT I don't know how to stop the loop if nothing is found -- the stuff in {{}} above. Can anyone tell me what value I need here?

    Thanks,

  2. #2
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Simple question (Word 2000/VBA)

    how about
    <pre>do while .execute ( parameters )
    your code here
    loop</pre>

    ?

  3. #3
    Lounger
    Join Date
    Jan 2001
    Location
    Missouri, USA
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Simple question (Word 2000/VBA)

    Thanks for the response, Peter. I've changed my code to:

    Do While .Execute(Selection.Find.Style = ActiveDocument.Styles("Sub-section"))
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Sub-section")
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    End With
    Selection.Find.Execute

    'if selection = found then
    Selection.Delete Unit:=wdCharacter, Count:=1
    Else
    End If
    Loop
    'loop until 'selection not found

    I'm getting "invalid or unqualified reference" errors on the "do while" line. I'm probably making a simple mistake. Any ideas?

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Simple question (Word 2000/VBA)

    If you look up the Found property of the Find object, I think you will satisfy the original question.

    However, it looks as though you might want to simplify your process with a Replace All (replacing the found text with nothing). In that case, re-recording the macro is the fastest way to get the code.

    If you need to do something fancier, then something like this should handle it:
    <pre>Sub FindLoopTest()
    Dim intCounter As Integer
    intCounter = 0

    With Selection.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("Sub-section")
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    Do While .Execute = True
    'fancy stuff here
    Selection.Delete Unit:=wdCharacter, Count:=1
    intCounter = intCounter + 1
    Loop
    End With

    MsgBox intCounter & " deletions made."
    End Sub</pre>

    As you can see, I put the loop around the call to the .Execute method. There are several ways to write this, but this one works so I'll stop here.

  5. #5
    Lounger
    Join Date
    Jan 2001
    Location
    Missouri, USA
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Simple question (Word 2000/VBA)

    Awesome! That's exactly what I needed. Thanks much.

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Simple question (Word 2000/VBA)

    One small warning.

    I had some almost identical code that appeared to work perfectly, until someone ran it on a document where the final paragraph in the document was the style I was searching for. It then went into an infinite loop.

    This example is safe - because you are deleting the found paragraph - but if someone copies this and does anything other than a delete then they may stumble on this one.

    StuartR

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Simple question (Word 2000/VBA)

    Good point. Perhaps the solution is to run the search "Up" the document from the end (.Forward = False and .Wrap = wdFindStop)? I haven't tried it myself.

Posting Permissions

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