Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-08-2020, 03:18 AM
alex100 alex100 is offline Find/Replace text longer than 255 characters Windows 7 64bit Find/Replace text longer than 255 characters Office 2016
Advanced Beginner
Find/Replace text longer than 255 characters
 
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
  #2  
Old 06-08-2020, 05:31 AM
gmaxey gmaxey is offline Find/Replace text longer than 255 characters Windows 10 Find/Replace text longer than 255 characters Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 06-08-2020, 03:48 PM
macropod's Avatar
macropod macropod is offline Find/Replace text longer than 255 characters Windows 7 64bit Find/Replace text longer than 255 characters Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #4  
Old 06-09-2020, 02:39 AM
alex100 alex100 is offline Find/Replace text longer than 255 characters Windows 7 64bit Find/Replace text longer than 255 characters Office 2016
Advanced Beginner
Find/Replace text longer than 255 characters
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Wonderful, thank you! No doubt many will find these useful as well.

Alex
Reply With Quote
  #5  
Old 07-08-2020, 12:41 AM
alex100 alex100 is offline Find/Replace text longer than 255 characters Windows 7 64bit Find/Replace text longer than 255 characters Office 2016
Advanced Beginner
Find/Replace text longer than 255 characters
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 07-08-2020, 04:14 AM
gmaxey gmaxey is offline Find/Replace text longer than 255 characters Windows 10 Find/Replace text longer than 255 characters Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

See:
Find & Replace (w\Long Strings)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/Replace text longer than 255 characters Combining Characters in Find & Replace Surge Word 6 03-10-2020 12:42 AM
Find/Replace text longer than 255 characters Insert text longer 254 characters in word Esgrimidor Word VBA 7 03-16-2017 01:02 PM
Find/Replace text longer than 255 characters Find & Replace tool no longer comes up Literalman Word 3 07-13-2015 03:07 PM
Find/Replace text longer than 255 characters 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft