From Access 2003, I need to open a .txt file, search for a text string, replace it with another text string, then save the .txt file to a different folder.
The file opens in Word then I get the "Runtime error 424: Object required" error. The debug stops at the line Selection.Find.ClearFormatting. I can't figure out what I'm missing.
My code is:
The file opens in Word then I get the "Runtime error 424: Object required" error. The debug stops at the line Selection.Find.ClearFormatting. I can't figure out what I'm missing.
My code is:
Code:
Private Sub WordTest_Click()
Dim LWordDoc As String
Dim oApp As Object
Dim objRange As Object
'Path to the word document
LWordDoc = "c:\Optio\export.txt"
If Dir(LWordDoc) = "" Then
MsgBox "Document not found."
Else
'Create an instance of MS Word
Set oApp = GetObject(Class:="Word.Application")
oApp.Visible = True
'Open the Document
oApp.Documents.Open filename:=LWordDoc
End If
'Replace +++ with page breaks
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "+++"
.Replacement.Text = "^m"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub