Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
The purpose of this Microsoft Word Tips & Microsoft Word Help page will show you how you can overcome the 255 character limit is Word built-in Find and Replace dialog "Find what:" and "Replace with:" text fields.
The 255 character replace string limit is easily overcome by copying the replace string text to the clipboard and using the character code "^c" in the "Replace with:" window of the dialog box or as the .Replacement.Text value in you VBA procedure code.
Another handy method for defining any replacement text string and even a graphic is to use AutoText. Word MVP Graham Mayor and I worked out the VBA which Graham has posted on his web site here: Replace w/AutoText. Or use the replace with AutoText option in VBA Find & Replace.
Unfortunately the character code ^c doesn't work in the "Find what" window. Even find strings constructed using wildcards are limited to 255 characters as shown below.
If you try to set a find or replace strings that are > 255 characters in length in VBA, a run-time error is generated.
I worked with Dave Lett and Helmut Weber, both former contributors the old Word VBA newsgroup, to develop a macro that would perform a find and replace routine where the text you are looking for exceeds 255 characters.
In this solution, both the find and replace strings are defined in a separate document. Here is the macro code:
Sub LongStringFindReplace() Dim oSourceDoc As Document Dim srchTxt As String Dim replaceRng As Range Dim i As Long Set oSourceDoc = Documents.Open(FileName:="C:\Long String Source.doc") 'Establish find string srchTxt = oSourceDoc.Paragraphs(1).Range.Text srchTxt = Left(srchTxt, Len(srchTxt) - 1) 'Remove paragraph mark 'Establish replace text and copy to clipboard Set replaceRng = oSourceDoc.Paragraphs(2).Range replaceRng.MoveEnd Unit:=wdCharacter, Count:=-1 'Remove paragraph mark replaceRng.Copy oSourceDoc.Close ActiveDocument.Range(0, 0).Select If Len(srchTxt) > 250 Then i = Len(srchTxt) - 250 With Selection.Find .Text = Left(srchTxt, 250) .Forward = True .Wrap = wdFindContinue Do While .Execute 'Move end of selection to match length of srchTxt Selection.MoveEnd Unit:=wdCharacter, Count:=i 'Compare selection to search string If Selection.Text = srchTxt Then 'Replace selction with clipboard contents Selection.Paste Else Selection.MoveRight End If Loop End With Else ResetFRParameters With Selection.Find .Text = srchTxt .Replacement.Text = "^c" .Forward = True .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With End If lbl_Exit: Exit Sub End Sub Sub ResetFRParameters() With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With lbl_Exit: Exit Sub End Sub
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.
That's it! I hope you have found this tips page useful and informative.
Do you want to make a payment for consulting work or donate to help support this site?
PayPal is a safe, easy way to pay online.
Use the appropriate currency "Donate" button to make a payment or donation.