View Single Post
 
Old 06-08-2020, 03:18 AM
alex100 alex100 is offline Windows 7 64bit Office 2016
Advanced Beginner
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default Find/Replace text longer than 255 characters

I am trying to replace certain tags with text that is longer than 255 characters. I found two possible solutions...

1) Greg Maxey's solution which involves copying/pasting data to clipboard. It's using '^c' in the 'Replacement.Text' instruction, as following:

.Replacement.Text = "^c"

source: Find & Replace (w\Long Strings)

I already implemented this solution, and it works great, but the problem is that in the end, the original clipboard content is lost. One way to overcome this problem is to save the original clipboard and then have it restored after the replacement code is run. I did that, and it works good for text content. But I have not found a solution to save and restore binary clipboard data!

Is there a way to do that in VBA, please? I'm referring to saving and restoring binary clipboard data, not just text.

2) I also found this code that uses .TypeText instead of .Replacement. You can use it with content that's longer than 255 characters.

Code:
With ActiveDocument.ActiveWindow.Selection
    .WholeStory
    .Find.ClearFormatting
    .Find.Execute FindText:="Whatever you want to replace"
    Options.ReplaceSelection = True
    .TypeText Text:="Type away to your heart's content..."
End With
source: vba - runtime error 5854 string parameter is too long - Stack Overflow

The problem is that at times it works ok, but most of the times it doesn't. For example, if you only have very little content inside the document (few words, basically), it works good. But if you have two or three pages, then what it does is to have the new text content inserted at the top of the document, not where the FindText content is located.

I think this second solution could work great too, with only a few adjustments to the code.

If anyone could help me, that would be great and much appreciated.

Thank you,

Alex
Reply With Quote