I do not know the term Python Code
But what I think that you need is:
Dim myrange As Range
Dim Findstr As String
Dim Replacementstr As String
Dim i as Long
For i = 0 to uBound(tokens)
Findstr = tokens(i)
Replacementstr = Values(i)
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=Findstr, Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) =
True
Selection.Range.Text = Replacementstr
Selection.Collapse wdCollapseEnd 'needed if the replacement text
contains the found item; won't hurt anyway
Loop
End With
Next i
If you need this operation performed on all of the story ranges of the
document, see the article "Using a macro to replace text where ever it
appears in a document including Headers, Footers, Textboxes, etc.†at:
http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Summer said:
Hello Doug,
I did not understand a few things in the code snippet you gave.
Is the While Loop not doing the same thing as having the find.wrap as
wdFindContinue ??
I am not able to understand what things are done special to handle
replacement string > 255 characters.
So for you to explain more, I am quoting my Find and Replace routine.
Python Code :
[code = python]
for r in document.StoryRanges:
r.Find.Text = tokens
/* Array of keyyords */
r.Find.Replacement.Text = Values /* Array of Replacement
Strings */
r.Find.Wrap = Word.WdFindWrap.wdFindContinue
r.Find.Execute(missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing,replaceAll, missing, missing, missing,
missing)
[/code]
Thanks Jean for your reply.
Doug Robbins - Word MVP said:
Some things are limited to 255 characters.
It should be possible to write code that gets around the problem by using
a
construction along the lines of
Dim myrange As Range
Dim Findstr As String
Dim Replacementstr As String
Findstr = InputBox("Insert the text that you want to find.")
Replacementstr = InputBox("Insert the replacement text.")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=Findstr, Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Selection.Range.Text = Replacementstr
Loop
End With
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.