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 )
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
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 )
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