I am attempting to set a word document to a class such that I can trap a document Close event.
The class is set within a Command Button click event.
The event sets the word.application, set the word.document, then attempts to set the document to a class which traps the document close.
The problem is that this works sometimes. Sometimes not. I can't tell why it works only sometimes. Naturally I need it to work all the time. Every time.
I have tried to set new instances of the document also, but that hasn't worked either.
Note that this is basically all that is contained in the DB, so there is nothing else related to this issue.
Code:
Option Compare Database
'*********************
' Class Module (Cls_Word)
'*********************
Option Compare Database
Option Explicit
Public WithEvents WdClsDoc As Word.Document
Private Sub WdClsDoc_Close()
'Purpose: Detect if a linked document is closed
MsgBox "Doc Close event"
End Sub
Code:
'*********************
' Form Module (Frm_Word)
'*********************
Option Compare Database
Option Explicit
Dim WdApp As Word.Application
Dim wdDocLink As Word.Document ' Or New Document, but that is still not consistant.
Dim ClsWord As New Cls_Word
Private Sub BtnLink_Click()
'Purpose: Links a word document(or application) with a Class.
Dim strFileName As String
DoCmd.Hourglass True
strFileName = "C:\My Documents\Word\DocNew.docx"
'Set Application
On Error Resume Next
Set WdApp = Nothing
Set WdApp = GetObject(, "Word.Application")
If WdApp Is Nothing Then
Set WdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
WdApp.Visible = True
WdApp.Activate
'Set Document
Set wdDocLink = WdApp.Documents.Open(FileName:=strFileName)
'Set Class
On Error GoTo ErrHandler
Set ClsWord.WdClsDoc = wdDocLink
On Error GoTo 0
Quitsub:
DoCmd.Hourglass False
Exit Sub
ErrHandler:
MsgBox "Class was not correctly set." & vbCrLf & Err.Description
GoTo Quitsub
End Sub
I should add that the resulting error is:
Code:
462 - The remote server machine does not exist or is unavailable.
Which I don't get because the server machine is set in module declarations
Cheers and thanks in advance for assistance.