Problem with Selection.Find -- Replace (Word 2003)

V

Vinland

Hi,
I am experiencing some sort of a problem using a macro-controlled search and
replace. I am using Selection.Find and have set .MatchWholeWord = True.
Although its set to True it doesn't work right ...

Example: There are two words in the document:
- SAHeOpplProddekn_To
- SAHeOpplProddekn_Tolv

The words after the underscores are norwegian for Two (To) and Twelve (Tolv).

The values to be run through the macro has been loaded into Word as
ActiveDocument.Variables and are named exactly the same as the two words are
(see above).

The trouble is that since SAHeOpplProddekn_To is run through the macro
first, and overwrites the word SAHeOpplProddekn_Tolv with example value
Grantedlv. Which gives two words in the document in place of the originally
words: Granted and Grantedlv the value for SAHeOpplProddekn_Tolv will not be
replaced into the document since the word no longer is there, overwritten as
it is by the value of SAHeOpplProddekn_To, and left with the lv from Tolv at
the end of the word Granted = Grantedlv.

I hope you get my point ...

Here's my macro. Is there anything I do wrong here? Any help or suggestion
will be appreciated :eek:)

Public Function FindAndReplace()
Dim objActiveDoc As Word.Document, aVar As Variable
On Error GoTo ErrorHandler
Set objActiveDoc = ActiveDocument
For Each aVar In objActiveDoc.Variables
With Selection.Find
.Format = True
.Text = aVar.Name
.Replacement.Text = aVar.Value
.Execute MatchCase:=True, MatchWholeWord:=True, Format:=True,
Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
Next aVar
objActiveDoc.UndoClear
Exit Function
ErrorHandler:
If Err.Number <> 0 Then
ErrorHandling Err.Number, Err.Description, "FindAndReplace"
End If
End Function
 
V

Vinland

Seems to me that I found the solution to the problem I presented.

Or, should I say, my girlfriend found the solution, although she ain't no
VBA-programmer. She understands logic though, and are able to see right
through the problem and point to what she suspects might be the cause.

The cause must be the underscore in the text, since, when I took away the
underscore from both the ActiverDocument.Variables-names and the text in the
document, and run the macro again, it seems the problem is gone.

Sincerely,
Paul Vinland
Norway
 

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