none
MS Word VBA search and replace RRS feed

  • Question

  • Is there an option within selection.find to ignore comments?

    I have a program which finds and replaces text in a large list of documents.  If one of the sought-after text snippets has a comment against it, the selection.find does not recognize that text as being the same as what is sought, so it doesnt get changed.

    Monday, June 18, 2018 6:59 PM

Answers

All replies

  • The Find method will search only the body of the document. To make it search other parts of the document such as comments, you need more complicated code - see https://wordmvp.com/FAQs/Customization/ReplaceAnywhere.htm

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, June 18, 2018 8:10 PM
  • Hello Nonya Bidness,

    According to the link Hans shared, we could try to loop through all the StoryRanges of the document and skip comment story range to do find/replacement.

    Such as 

    Dim rngStory As Range
      For Each rngStory In ActiveDocument.StoryRanges
      If rngStory.StoryType <> wdCommentsStory Then
        With rngStory.Find
          .Text = "LIST"
          .Replacement.Text = "Test"
          .Wrap = wdFindContinue
          .Execute Replace:=wdReplaceAll
        End With
       End If
      Next rngStory

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 19, 2018 5:53 AM
  • The Find method will search only the body of the document. To make it search other parts of the document such as comments, you need more complicated code - see https://wordmvp.com/FAQs/Customization/ReplaceAnywhere.htm

    This is actually the code I am using.  The problem is not finding text in a comment, its finding text anywhere (body, header, whatever) that is highlighted and commented.  IOW, if I am trying to find SEARCHTEXT to replace it with FOUNDTEXT, and somewhere in the body of the document is SEARCHTEXT it will be found and replaced.  But somewhere else within the body the SEARCHTEXT is highlighted within a comment (I dont mean its in the body of a comment, its just commented upon!), it will not be found even with the wordmvp code.  Apparently SEARCHTEXT and SEARCHTEXT commented upon are different.

    Its quite frustrating and seems like a real shortcoming of the Word VBA design.  Search and replace should search and replace ALL (perhaps with appropriate switches), not just the body.  We shouldn't be having to write all kinds of looping code to do a complete S&R.

    Thanks for the insight, however.  Apparently there is no easy solution to this!!

    Tuesday, June 19, 2018 1:03 PM
  • Hello Nonya Bidness, As far as I know, Word does not provide function/property to find/replace text in the whole document. We have to loop through all the story ranges if we want to find/replace in all of the document. You could create a custom function to loop in the whole document and then call the function so that your code will be more simple. Best Regards, Terry

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 20, 2018 7:40 AM