Search and Replace text returned Exception

S

Summer

Hello,

I had this Python script working fine that searches for some keywords in a
Word document and Replace them with defined values.

Suddenly the Replace failed for a particular 'string' and the only
conclusion I could come is that the length of the Replacement String is
probably exceeding the permitted limit.

[By cropping the Replacement string by a few characters, I found the program
to be running fine]

Is my conclusion correct? Any idea what this maximum length is ?
Does the 'Find.Execute' method return the exception info when there is an
exception ?

Thanks !
 
J

Jean-Guy Marcil

Summer was telling us:
Summer nous racontait que :
Hello,

I had this Python script working fine that searches for some keywords
in a Word document and Replace them with defined values.

Suddenly the Replace failed for a particular 'string' and the only
conclusion I could come is that the length of the Replacement String
is probably exceeding the permitted limit.

[By cropping the Replacement string by a few characters, I found the
program to be running fine]

Is my conclusion correct? Any idea what this maximum length is ?
Does the 'Find.Execute' method return the exception info when there
is an exception ?

Thanks !

The maximum length is 256 characters.
 
D

Doug Robbins - Word MVP

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.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

Summer

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.
 
D

Doug Robbins - Word MVP

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top