trueharted
Programmer
I've taken code from a Word macro and combined it with some other VBA code to open a Word document and find and replace text based on user input. The code looks to a table to find the location of the file. I've referenced Microsoft Word 9.0 Object Library.
Originally when I stepped through the code, when I got to the line:
.Text=strFindText
I got an Access generated errors and will terminate error. (I tried this about five times and got the same error, even after rebooting.)
Just now I stepped through it again and it executed all the way through and successfully opened the document. However when it finished and I looked at the document, the word hadn't been replaced!
I even tried hard coding the find text and replacement text instead of having the user input it and it still didn't work.
What am I doing wrong here?
==================================
Public Function FindAndReplace()
On Error GoTo FindAndReplace_Err
Dim callWordMerge As Variant
Dim appWord As New Word.Application
Dim doc As Word.Document
Dim rst As ADODB.Recordset
Dim DB As Database
Dim RS As Recordset
Dim vPath As String
Dim vDoc As String
Dim vFullPath As String
Dim strReplaceText As String
Dim strFindText As String
On Error Resume Next
Set DB = CurrentDb()
Set RS = DB.OpenRecordset("FindDocument"
Set appWord = GetObject(, "Word.application"
If Err = 429 Then
Set appWord = New Word.Application
Err = 0
End If
RS.MoveFirst
Do Until RS.EOF
Set rst = Nothing
Set doc = Nothing
Set appWord = Nothing
vPath = RS("Path"
vDoc = RS("Document"
vFullPath = vPath & vDoc
With appWord
Set doc = .Documents(vDoc)
If Err = 0 Then
If MsgBox("Do you want to save the current document before updating the data?", vbYesNo) = vbYes Then
.Dialogs(wdDialogFileSaveAs).Show
End If
doc.Close False
End If
Set doc = .Documents.Open(vFullPath, , True)
Set rst = New ADODB.Recordset
appWord.Documents.Open vFullPath
appWord.Visible = True
With doc
strFindText = InputBox("type find text"
strReplaceText = InputBox("type replace text"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = strFindText
.Replacement.Text = strReplaceText
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
End With
.Visible = True
.Activate
End With
RS.MoveNext
Loop
vPath = ""
RS.Close
DB.Close
Beep
MsgBox "Complete!"
Err.Clear
FindAndReplace_Exit:
Exit Function
FindAndReplace_Err:
MsgBox Err.Description
End Function
Originally when I stepped through the code, when I got to the line:
.Text=strFindText
I got an Access generated errors and will terminate error. (I tried this about five times and got the same error, even after rebooting.)
Just now I stepped through it again and it executed all the way through and successfully opened the document. However when it finished and I looked at the document, the word hadn't been replaced!
I even tried hard coding the find text and replacement text instead of having the user input it and it still didn't work.
What am I doing wrong here?
==================================
Public Function FindAndReplace()
On Error GoTo FindAndReplace_Err
Dim callWordMerge As Variant
Dim appWord As New Word.Application
Dim doc As Word.Document
Dim rst As ADODB.Recordset
Dim DB As Database
Dim RS As Recordset
Dim vPath As String
Dim vDoc As String
Dim vFullPath As String
Dim strReplaceText As String
Dim strFindText As String
On Error Resume Next
Set DB = CurrentDb()
Set RS = DB.OpenRecordset("FindDocument"
Set appWord = GetObject(, "Word.application"
If Err = 429 Then
Set appWord = New Word.Application
Err = 0
End If
RS.MoveFirst
Do Until RS.EOF
Set rst = Nothing
Set doc = Nothing
Set appWord = Nothing
vPath = RS("Path"
vDoc = RS("Document"
vFullPath = vPath & vDoc
With appWord
Set doc = .Documents(vDoc)
If Err = 0 Then
If MsgBox("Do you want to save the current document before updating the data?", vbYesNo) = vbYes Then
.Dialogs(wdDialogFileSaveAs).Show
End If
doc.Close False
End If
Set doc = .Documents.Open(vFullPath, , True)
Set rst = New ADODB.Recordset
appWord.Documents.Open vFullPath
appWord.Visible = True
With doc
strFindText = InputBox("type find text"
strReplaceText = InputBox("type replace text"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = strFindText
.Replacement.Text = strReplaceText
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
End With
.Visible = True
.Activate
End With
RS.MoveNext
Loop
vPath = ""
RS.Close
DB.Close
Beep
MsgBox "Complete!"
Err.Clear
FindAndReplace_Exit:
Exit Function
FindAndReplace_Err:
MsgBox Err.Description
End Function