[AccessD] Problem with Opening a Word document from within Access

Arthur Fuller fuller.artful at gmail.com
Mon Jul 20 08:30:28 CDT 2009


This one is very bizarre, folks. The app in question has been in use for
several years and the following code snippet now fails on my home machine.
Basically it opens a Word template file and then fills its bookmarks with
values from Access.

Now, for some reason, I am able to run this code successfully. The template
file exists and I can open it manually from Word without problems. But the
following code snippet fails on the last line, when it attempts to get the
bookmark count. The reason is that the document is not open. Word is open
and visible, but the document is not loaded. I have no idea why this is
happening. Do you have any suggestions?

<vba code>

    Set objWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
      Set objWord = CreateObject("Word.Application")
    End If
    ' Launch Word and load the invoice template
    On Error GoTo Create_PSR_Doc_Error

    'Make sure we have a Word Instance
    If objWord Is Nothing Then
      Err.Raise 65355, "Create_PSR_Doc", "Word is not available"
    End If
    'Set objWord = New Word.Application
    Dim doc As Word.Document
    Dim strTemplateFile As String

    strTemplateFile = "C:\DSA\Document
Templates\PSR_Report_Template_20060223.dot"   'TEMPLATEPATH
    Set doc = objWord.Documents.Add(strTemplateFile)
    objWord.Visible = True
    ' At this point, Word is visible, but the document is not loaded
    Debug.Print "Bookmark count:", objWord.ActiveDocument.Bookmarks.Count
    ' Code crashes on the line above
</vba code>

It gets stranger. I just inserted a break point on the third last line (the
"Set doc" line) and began to single-step. After running this line, the doc
variable is nothing. I am deeply puzzled by this. Any suggestions you might
have would be greatly appreciated. I'm going to try this code on my notebook
computer and see if it has the same strange behaviour.

Thanks,
Arthur



More information about the AccessD mailing list