View Single Post
 
Old 02-02-2022, 03:30 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I think you put the wholeword line in the wrong place. Try this variation on Graham's code
Code:
Sub PrintSpecificWordsII()
  Dim myWords() As String
  Dim lngIndex As Long
  Dim oStory As Range
  myWords = Split("Word1|Word2|Word3|Word4|Word5", "|")
  For lngIndex = LBound(myWords) To UBound(myWords)
      For Each oStory In ActiveDocument.StoryRanges
          With oStory.Find
              .ClearFormatting
              .Text = myWords(lngIndex)
              .Forward = True
              .MatchWholeWord = True
              While .Execute
                  oStory.HighlightColorIndex = wdBrightGreen
                  oStory.Select
                  Application.PrintOut Range:=wdPrintCurrentPage
                  oStory.Collapse wdCollapseEnd
              Wend
          End With
          If oStory.StoryType <> wdMainTextStory Then
              While Not (oStory.NextStoryRange Is Nothing)
                  Set oStory = oStory.NextStoryRange
                  With oStory.Find
                      .ClearFormatting
                      .Text = myWords(lngIndex)
                      .Forward = True
                      .MatchWholeWord = True
                      While .Execute
                          oStory.HighlightColorIndex = wdBrightGreen
                          oStory.Select
                          Application.PrintOut Range:=wdPrintCurrentPage
                          oStory.Collapse wdCollapseEnd
                      Wend
                  End With
              Wend
          End If
      Next oStory
  Next lngIndex
lbl_Exit:
  Set oStory = Nothing
  Exit Sub
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote