I have a word document that contains normal text, tables and shapes. I have a macro that runs through an excel list and replaces certain texts in the word doc with other data. So it would replace <name> with Bob Smith for example.
The problem is some data strings are longer than 255 characters. So I need to replace <name> with the first 255 characters. SELECT the text at that point and insert the other data via numerous strings.
The problem is when using this code how do I select the range in order to be able to insert the data. Find / selection only moves the cursor when finding via selection. When finding via storey range word doesn't move the cursor to that point.
Here is an extract of my code:
For Each myStoryRange In ActiveDocument.StoryRanges myStoryRange.Select With myStoryRange.Find .Text = input_text .Replacement.Text = Mid(output_text, 1, 255) .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With If myStoryRange.Find.Execute = True Then myStoryRange.Find.Execute Replace:=wdReplaceAll Set rng = doc.Range(myStoryRange.End, myStoryRange.End) rng.InsertAfter " Inserted text" End If Do While Not (myStoryRange.NextStoryRange Is Nothing) Set myStoryRange = myStoryRange.NextStoryRange With myStoryRange.Find .Text = input_text .Replacement.Text = Mid(output_text, 1, 255) .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Loop
in Word there's no limitation to string lengths.
I would prefer:
Sub snb() For Each sr In ThisDocument.StoryRanges sr.Find.Execute "text", , , , , , , , , "replacement text", wdReplaceAll Next End Sub
Thanks for the assistance.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks