Word document Find/Replace problem

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
 
R

rogsonl1

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

I am having the same problem with a simple macro to find and replace
in strings when accessing word from msAccess.
I am looking for a work around, but could use a pointer to a solution.
Leon
 

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