Replacement object generates errors

T

Ture Magnusson

Hi all!

This VBA procedure is run from Excel. I have referred to MS Word 10.0
object library, and everything works OK until it reaches the line
Set rpl = doc.Find.Replacement
which generates runtime error 438 - Object doesn't support this property or
method.

(I started up using a simpler With / End With construct, and it resulted in
the same
error when it tried to set the .Find.Replacement.Text)

So - what am I missing?

Sub ReplaceSomeTextInOpenWordDocWithNothing()
'Declare object variables
Dim wd As Word.Application
Dim doc As Word.Document
Dim fnd As Word.Find
Dim rpl As Word.Replacement

'Refer to open Word application
On Error Resume Next
Set wd = GetObject(Class:="Word.Application")
On Error GoTo 0
If wd Is Nothing Then
MsgBox "Microsoft Word is not running."
Exit Sub
End If

'Refer to Word's active document
On Error Resume Next
Set doc = wd.ActiveDocument
On Error GoTo 0
If doc Is Nothing Then
MsgBox "Microsoft Word has no active document."
Set wd = Nothing
Exit Sub
End If

'Refer to the range's Find and Replacement objects
Set fnd = doc.Range.Find
Set rpl = doc.Find.Replacement

'Replace (s) with nothing
fnd.ClearFormatting
fnd.Text = "(s)"
fnd.Forward = True
fnd.Wrap = wdFindContinue
fnd.Format = False
fnd.MatchCase = False
fnd.MatchWholeWord = False
fnd.MatchWildcards = False
fnd.MatchSoundsLike = False
fnd.MatchAllWordForms = False

rpl.ClearFormatting
rpl.Text = ""

fnd.Execute Replace:=wdReplaceAll

'Release object variables
Set rpl = Nothing
Set fnd = Nothing
Set doc = Nothing
Set wd = Nothing
End Sub

Ture Magnusson
Karlstad, Sweden
 

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