Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Jun 2001
    Posts
    146
    Thanks
    0
    Thanked 0 Times in 0 Posts

    End of Document (VBA/Word 2000 SP-3)

    Hi,

    In a Word doc, I'm trying to add text to all paragraphs that are in the Heading 3 style. I've tried the following code, but the loop is only performing once, rather than working on all Heading 3 paragraphs.

    ******************************

    Sub AddTextToHeadingText()

    'Create range

    Dim AllDoc As Range
    Set AllDoc = ActiveDocument.Range

    'Go to beginning of document

    Selection.HomeKey Unit:=wdStory

    'Perform loop until end of document

    Do

    'Search for heading 3 text

    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 3,H3")
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.EndKey Unit:=wdLine
    Selection.TypeText Text:=" -- New Text"

    Loop Until AllDoc.End

    End Sub

    ******************************

    Thanks,
    Bob

  2. #2
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Quakertown, PA, Pennsylvania, USA
    Posts
    517
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: End of Document (VBA/Word 2000 SP-3)

    Try adding this, MrBob:

    If Selection.Find.Found = True Then
    'Do something within the found text
    Else
    Exit Do
    End If

    Because the selection won't be found anymore, correct?

  3. #3
    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: End of Document (VBA/Word 2000 SP-3)

    Sorry about that first post, my phone seems to have truncated it. Now that I have a real computer to work from, here's the complete idea:

    Word's wildcard search feature might work for this; a Replace All solution is easiest if you can get the wildcard syntax nailed down.

    If you look up the .End property, you'll see why your loop exits: it returns a large number rather than a boolean (true/false) value. You will have better results using an If test as described by Dreamboat's post. Also, you might need to change the wdFindContinue constant to wdFindStop to avoid infinite looping.

  4. #4
    2 Star Lounger
    Join Date
    Jun 2001
    Posts
    146
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: End of Document (VBA/Word 2000 SP-3)

    The If Selection.Find.Found logic helped get what I wanted.

    Thanks,
    Bob

  5. #5
    2 Star Lounger
    Join Date
    Jun 2001
    Posts
    146
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: End of Document (VBA/Word 2000 SP-3)

    Also, I ended up searching for a paragraph marker in a certain style. I then replaced it with the added text and a new paragraph marker.

    Bob

Posting Permissions

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