Share via

Find and replace multiple lines/paragraphs

Anonymous
2011-11-17T17:01:40+00:00

Within a Word document, I have a particualar paragraph repeated numerous times.  I would like to use find and replace, put the original paragraph in the "Find" field and place a completely different paragraph in the "Replace" field.

How can this be accomplished?

I have seen other tools which have this ability in .txt files and the like, but I would prefer to do this within Word, as I do not want to lose the formating in the original dcument.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
2011-11-18T21:01:51+00:00

Testing here, it seems that the VBA Replace() command is not subject to a character limit.  Therefore, you can use:

Dim strFind As String

Dim strReplace As String

strFind = ""

strReplace = ""

With ActiveDocument.Range

    .Text = Replace(.Text, strFind, strReplace)

End With

where you copy and paste the text to be found into the strFind = "" and the replacement text into the strReplace = "" such as:

Dim strFind As String

Dim strReplace As String

strFind = "On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look. You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly."

strReplace = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci."

With ActiveDocument.Range

    .Text = Replace(.Text, strFind, strReplace)

End With

Was this answer helpful?

7 people found this answer helpful.
0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Suzanne S Barnhill 278K Reputation points MVP Volunteer Moderator
    2011-11-17T17:27:47+00:00

    I would not have thought this would work, but in fact you can copy and paste an entire paragraph of text into the "Find what" box and Word will not choke. After doing this copy the replacement paragraph and use ^c (Clipboard contents) in the "Replace with" box. I suppose you could equally well paste the replacement paragraph in as well, but let's give Word a break here! (Also, that method will preserve your formatting.)

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Suzanne S Barnhill 278K Reputation points MVP Volunteer Moderator
    2011-11-18T00:46:54+00:00

    Okay, I'll confess my experiment was not very scientific because I was using lorem ipsum dummy text, and it's so repetitive and meaningless that it's hard to tell what you're really doing. My first thought was that there might be some way to find the specified text using wildcards, and that may still be possible.

    Was this answer helpful?

    0 comments No comments
  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Anonymous
    2011-11-17T18:12:49+00:00

    Suzanne,

    That may work and probably will in many cases but it is not full proof.  Only the first 255 characters entered in the "Find" field are valid.  So if you had:

    This is a paragraph with the first 255 characters the same as another paragraph ..... call it paragraph 1 with the ending "AAAAA"

    This is a paragraph with the first 255 characters the same as another paragraph ..... call it paragraph 2 with the ending "ZZZZZ"

    The two paragraphs are clearly not the same.  If you copy either and paste the content in the Find What field then both will be found on execute and only the first 255 characters will be replaced.

    So if the paragraphs you want to find are larger than 255 characters this method won't work.

    To work realiably you need a macro where you can define the find and replace text in a source document:

    http://gregmaxey.mvps.org/Find_Long_String.htm

    Was this answer helpful?

    0 comments No comments