![]() |
|
#1
|
|||
|
|||
|
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
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 |
|
#2
|
|||
|
|||
|
Maybe:
Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strR As String
Dim oRng As Range
strR = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "XXX"
While .Execute
oRng.Select
Selection.TypeText strR
oRng.Collapse wdCollapseEnd
Wend
End With
lbl_Exit:
Exit Sub
End Sub
|
|
#3
|
||||
|
||||
|
Somewhat simpler and, by avoiding selections and screen updating, way faster:
Code:
Sub Demo()
Application.ScreenUpdating = False
Const strR As String = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" _
& "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
With ActiveDocument.Range
.Find.Text = "XXX"
Do While .Find.Execute
.Text = strR
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#4
|
|||
|
|||
|
Wonderful, thank you! No doubt many will find these useful as well.
Alex |
|
#5
|
|||
|
|||
|
Greg, Paul, I am wondering if your solutions above may be adapted so they can also find strings that are longer than 255 characters.
Indeed, they can use replacement strings without this limit, but what about expanding the limit for the text to find...? Alex |
|
#6
|
|||
|
|||
|
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Combining Characters in Find & Replace
|
Surge | Word | 6 | 03-10-2020 12:42 AM |
Insert text longer 254 characters in word
|
Esgrimidor | Word VBA | 7 | 03-16-2017 01:02 PM |
Find & Replace tool no longer comes up
|
Literalman | Word | 3 | 07-13-2015 03:07 PM |
Find and Replace some characters with Bullets
|
kjxavier | Word | 1 | 01-02-2015 12:15 AM |
| Find and replace No longer work | TJH | Word | 3 | 03-25-2014 11:33 PM |