J
jliu1971
Hi,
Please help me solve a Word document find/replace problem.
Background: Windows XP, MS Word 2002(sp3), and Visual Basic 6 (sp5).
My project has one form with one command button:
Private Sub Command1_Click()
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
On Error GoTo Exit_Command1_Click
Set WordApp = New Word.Application
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open("C:\myTest.doc", , False)
WordDoc.Activate
Dim rng As Word.Range
Set rng = WordDoc.Content
rng.Find.ClearFormatting
rng.Find.Replacement.ClearFormatting
With rng.Find
.Text = "foo"
.Replacement.Text = "bar"
.Execute Replace:=wdReplaceAll, _
Format:=True, MatchCase:=True, _
MatchWholeWord:=True
End With
WordDoc.SaveAs "C:\myTest2.doc"
Exit_Command1_Click:
If Err.Number <> 0 Then
MsgBox Err.Number & ", " & Err.Description
End If
WordDoc.Close False
Set WordDoc = Nothing
WordApp.Quit
Set WordApp = Nothing
MsgBox "done"
End Sub
The program bombed at line:
rng.Find.Replacement.ClearFormatting
where it said "Visual Basic has encountered a problem and needs to
close...." Has anyone had the same issue? This is a simple example
straight out of the help menu. It must be something simple that I
overlooked.
myTest.doc has three words: foo foo foo.
Thanks in advance,
Jen
Please help me solve a Word document find/replace problem.
Background: Windows XP, MS Word 2002(sp3), and Visual Basic 6 (sp5).
My project has one form with one command button:
Private Sub Command1_Click()
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
On Error GoTo Exit_Command1_Click
Set WordApp = New Word.Application
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open("C:\myTest.doc", , False)
WordDoc.Activate
Dim rng As Word.Range
Set rng = WordDoc.Content
rng.Find.ClearFormatting
rng.Find.Replacement.ClearFormatting
With rng.Find
.Text = "foo"
.Replacement.Text = "bar"
.Execute Replace:=wdReplaceAll, _
Format:=True, MatchCase:=True, _
MatchWholeWord:=True
End With
WordDoc.SaveAs "C:\myTest2.doc"
Exit_Command1_Click:
If Err.Number <> 0 Then
MsgBox Err.Number & ", " & Err.Description
End If
WordDoc.Close False
Set WordDoc = Nothing
WordApp.Quit
Set WordApp = Nothing
MsgBox "done"
End Sub
The program bombed at line:
rng.Find.Replacement.ClearFormatting
where it said "Visual Basic has encountered a problem and needs to
close...." Has anyone had the same issue? This is a simple example
straight out of the help menu. It must be something simple that I
overlooked.
myTest.doc has three words: foo foo foo.
Thanks in advance,
Jen