Open Word document

kadara

Registered User.
Local time
Today, 20:19
Joined
Jun 19, 2010
Messages
43
Hi,

I would like to open a word document from an access form (using a button). I get a runtime error message: "Run-time error 429 - ActiveX component can't create object".

I try to use a simple code:

Code:
Private Sub Command0_Click()
Dim wrd As Object
Set wrd = GetObject(, "Word.Application")
wrd.Visible = True
wrd.Documents.Open "C:\My Documents\Temp01.doc"
Set wrd = Nothing
End Sub

Please help.
Thanks
 
Word needs to be open for GetObject to work. If you want to open a new instance of Word you use CreateObject().

And I would advise you open the doc first before making the word app visible.
 
Ok.
In fact I would like to update an existing word document with some information from an access table with the following code:

Code:
Public Function wrdUpdate(tmpDocName As String, tmpDocPath As String, tmpSeria, tmpNume, tmpData As Date)
 
Dim tmpAppWord As Object
Dim tmpDoc As Object
On Error Resume Next

Set tmpAppWord = GetObject(, "Word.Application")
 
If Err = 429 Then
    Set tmpAppWord = New Word.Application
    Err = 0
End If
 
With tmpAppWord
    Set tmpDoc = .Documents(tmpDocName)
    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
    tmpDoc.Close False
    End If
 
    On Error GoTo errorhandler
    
    Set tmpDoc = .Documents.Open(tmpDocPath & tmpDocName, , True)
    With tmpDoc
        .FormFields("fldSeria").Result = tmpSeria
        .FormFields("fldNume01").Result = tmpNume
        .FormFields("fldData01").Result = Format(tmpData, "Short Date")
        .FormFields("fldData02").Result = Format(tmpData, "Short Date")
        .FormFields("fldData03").Result = Format(tmpData, "Short Date")
    End With
    .Visible = True
    .Activate
End With
 
Set tmpDoc = Nothing
Set tmpAppWord = Nothing

Exit Function
errorhandler:
MsgBox Err & Err.Description

End Function

And when I execute this code, get the 429 error message.
 
Is this your code or a copy//paste from somewhere?

Like I mentioned before, if the document isn't open at the time you're running the code then you need CreateObject(). Research that.
 
I updated an existing code to my project.
I changed the GetObject to CreateObject, and get an "Runtime Error 4160: Bad file name" on the
Code:
Set doc = .Documents(tmpDoc)
line.
 
Just like the error says, bad file name. Checkthe path.
 
Nothing wrong with the file name or the path.
 
I tried to open the same doc file from the local drive (C:\Test\MA040.doc), but I got the same error message (Run-time error 4160: Bad file name)
 
Alright, I didn't notice what you were doing there. You are using the wrong command to open the document. So change this:
Code:
    Set tmpDoc = .Documents(tmpDocName)
to this:
Code:
    Set tmpDoc = .Documents[COLOR=Red].Open[/COLOR](tmpDocName)
Again, your code was made for working with documents that are open and that line was referring to the index of an already open word doc.
 

Users who are viewing this thread

Back
Top Bottom