Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Mar 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find not finding style (ENU/Word/2003)

    Hi All,

    I need to find a style in a document and replace it with another style and add a few characters to the beginning of the paragraph. The macro starts out fine but then when there are no more occurrences of the style, it continues anyway. Can anyone tell me what I might be missing?

    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Bullet List 1")
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    End With
    Selection.Find.Execute
    While Selection.Find.Found
    With Selection
    .Style = ActiveDocument.Styles("Paragraph")
    .HomeKey Unit:=wdLine
    .TypeText Text:=vbTab & "* "
    .MoveDown Unit:=wdParagraph, Count:=1
    End With
    Selection.Find.Execute
    Selection.Find.Style = ActiveDocument.Styles("Bullet List 1")
    With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    End With
    Selection.Find.Execute
    Wend

    Thanks much.

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find not finding style (ENU/Word/2003)

    I thought that I posted a response here sometime between 05:00 and 06:30 today.
    Oh well, here's a perhaps better response, if it does what the OP wanted.

    <pre> Dim rngFound As Word.Range
    Set rngFound = ActiveDocument.Content
    With rngFound
    With .Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("Heading 1")
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Execute
    While .Found
    With rngFound
    .Paragraphs(1).Range.InsertBefore vbTab & "* "
    .Collapse Direction:=wdCollapseEnd
    End With
    .Execute
    Wend
    End With
    End With
    Set rngFound = Nothing
    </pre>


  3. #3
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find not finding style (ENU/Word/2003)

    Hello,

    Here's a slightly simpler way, using the "^&" special character, which stands for "whatever text was matched by the search". Using Replace All is also typically faster than examining each matched range and making a change.

    Hope this helps!

    <pre>Sub RetagBulletListAndAddTab()
    Dim doc As Document
    Set doc = ActiveDocument
    With doc.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = True
    .MatchWildcards = False
    .Style = doc.Styles("Bullet List 1")
    .Text = vbNullString
    .Replacement.Style = doc.Styles("Paragraph")
    .Replacement.Text = "^t*^&"
    .Execute Replace:=wdReplaceAll
    End With
    End Sub
    </pre>


  4. #4
    New Lounger
    Join Date
    Mar 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find not finding style (ENU/Word/2003)

    Thanks. Both options worked! This is a great resource!

Posting Permissions

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