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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error when using automation to open Word...

Status
Not open for further replies.

tomvdduin

Programmer
Sep 29, 2002
155
NL
Hi!

I'm getting an error when I call the function CreateWordMemo. It breaks at line: .Documents.Open "c:\ch14 memo.dot" I have replaced it with .Documents.Add "c:\ch14 memo.dot" but I get the same error.

Now the strange part: When I set a breakpoint at the beginning of the code and I step through it, line by line, it doesn't give the error!

Am I doing something wrong or is something wrong in the setup of Access/Word?

This is the error I'm getting (I'm using the dutch version of Office, so I translated the error):
"Error -2147417851 (80010105) during the presentation:
Method Open of object Documents failed"

Something interesting: Word DOES start, it only doesn't load the template. When I close word Windows XP says that word has has an fault ans shall be closed. The next time I want to open the template, word tries me to beleve it's the fault of the template that word crashed, but nothing's wrong with the template!

I hope someone can help me. I'm using Access XP, Word XP and Windows XP PRO.

Greetz, Tom

this is the code:

Public Function CreateWordMemo()
' Open a memo in Word and insert text - used by menu command.

' Declarations
Dim rstEmployees As New ADODB.Recordset
Dim appWord As New Word.Application

' Open a recordset based on the EmployeesWithOpenIssues query.
rstEmployees.Open "EmployeesWithOpenIssues", _
CurrentProject.Connection, adOpenKeyset, adLockOptimistic

' If no one has open issues, display a message and exit.
If rstEmployees.RecordCount = 0 Then
DisplayMessage "There are no open issues to announce."
Exit Function
End If

' Open document in word, goto bookmark
With appWord
.Visible = True
.Documents.Open "c:\ch14 memo.dot"
.ActiveDocument.ShowSpellingErrors = False
.Selection.GoTo wdGoToBookmark, Name:="MemoToLine"
End With

' insert employee names in memo
Do Until rstEmployees.EOF
appWord.Selection.TypeText rstEmployees!EmployeeName & " "
rstEmployees.MoveNext
Loop
' Save word document
appWord.ActiveDocument.SaveAs "C:\tom3.doc"
' close the word document
appWord.ActiveDocument.Close
'close word
appWord.Quit

End Function
 
I usually try to automate without making the program visible unless I want the user to see it before it is closed. See if these changes will help (I changed a couple of recordset variables to work with my database):

Code:
Public Function CreateWordMemo()
' Open a memo in Word and insert text - used by menu command.
    
    ' Declarations
    Dim rstCust As New ADODB.Recordset
    Dim appWord As Word.Application
    Dim doc As Word.Document
    
    ' Open a recordset based on the EmployeesWithOpenIssues query.
    rstCust.Open "Customers", _
        CurrentProject.Connection, adOpenKeyset, adLockOptimistic

    ' If no one has open issues, display a message and exit.
    If rstCust.RecordCount = 0 Then
        MsgBox "There are no open issues to announce."
        Exit Function
    End If
    
    Set appWord = New Word.Application
    Set doc = appWord.Documents.Open("C:\ch14 memo.dot", _
        False, False, False, , , , , , , , False)
    
    With doc
      .ShowSpellingErrors = False
      .ShowGrammaticalErrors = False
      .Bookmarks("MemoToLine").Select
    End With
    
    ' insert employee names in memo
    Do Until rstCust.EOF
        appWord.Selection.TypeText rstCust!CustName & "  "
        rstCust.MoveNext
    Loop
    ' Save word document
    doc.SaveAs "C:\tom3.doc"
    ' close the word document
    doc.Close
    'close word
    appWord.Quit
    
End Function

VBSlammer
redinvader3walking.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top