Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Runtime Error 424: Object required 1

Status
Not open for further replies.

MarjiS

Programmer
Jul 5, 2005
13
US
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:

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

 
You are not qualifying the methods
Code:
    [red]oApp.[/red]Selection.Find.ClearFormatting
    [red]oApp.[/red]Selection.Find.Replacement.ClearFormatting
    With [red]oApp.[/red]Selection.Find
        .Text = "+++"
        .Replacement.Text = "^m"
        .Forward = True
        .Wrap = wdFindContinue
    End With
    [red]oApp.[/red]Selection.Find.Execute Replace:=wdReplaceAll
 
*sighs*
You know, I tried that and it didn't work.
Then I rebooted and it did.

Thank you!
 
Hm. Now, I have an open Word window with the first "+++" highlighted but not replaced. Any idea there?
 
I assume that it's still open because you didn't close it ... at least not that I can see.

Hit Ctrl-Alt-Del and look at what you have running. There may be multiple instances of Word.
 
No joy. When that file opens, it's the only instance of Word running.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top