Catching Word Events w/ Access

F

Fly Girl

I'm trying to capture events in an instance of Word from my MS Access
app. What I have so far will open the file, but the DocumentBeforeSave
event doesn't fire.

In a class module I have:
________________________________________________________________________________

Public WithEvents appWord As Word.Application
Public WithEvents docWord As Word.Document

Private Sub appWord_DocumentBeforeSave(ByVal Doc As Word.Document,
SaveAsUI As Boolean, Cancel As Boolean)

MsgBox "This record is locked. You cannot save this file.", _
vbInformation + vbOKOnly, "Locked Record"
Cancel = True

End Sub
________________________________________________________________________________

In my code module I'm doing the following (non-relevant portions
omitted):
_______________________________________________________________________________

Public Function OpenFile()
On Error GoTo Err_OpenFile

Dim frm As Form
Dim bolWordOpen As Boolean
Dim clsWord As New clsMSWord
Dim strFile As String

Set frm = Screen.ActiveForm

If frm![EventsSub].Form!FileType = "doc" Then

Set clsWord.appWord = CreateObject("Word.Application")
clsWord.appWord.Visible = True

strFile = frm![EventsSub].Form.txtLinkFilePath

' Check to see if the record is locked. Open the file
appropriately.
If frm![EventsSub].Form.Locked Then

' Open the document
Set clsWord.docWord =
clsWord.appWord.Documents.Open(strFile, , , _
False, , , , , , , ,
True)
End If

Else
' Otherwise let Access OLE open the attached file
frm![EventsSub].Form.oleLinked.Action = acOLEActivate
End If

Exit Function

Err_OpenFile:

Call adcRunTimeErr(Err.Number, Err.Description, "OpenFile")

End Function
_______________________________________________________________________________

Any suggestions? Thanks!
 
P

Perry

Replace
Set clsWord.appWord = CreateObject("Word.Application")

by
Set clsWord.appWord = GetObject(, "Word.Application")
or even by
Set clsWord.appWord = Word.Application

In yr situation, it looks like the DocumentBeforeSave event
is fired in another (new) instance of Word. (CreateObject ...)

Krgrds,
Perry
 
T

Tushar Mehta

You need to move the

Dim myWord As clsMSWord

out of the function. Make it part of the module declaration as in

Dim myWord As clsMSWord
Public Function OpenFile()
...

With the myWord variable local to OpenFile, when that function ends,
the variable is deallocated.

Even with it global, there were two subtle problems that surfaced in my
tests. First, the Word window was active when I tried to save the
file. All I heard was a beep and the cursor remained an hourglass.

The reason was that the warning from the appWord_DocumentBeforeSave
MsgBox was in the *Access* window. You might want to add a

AppActivate Application.name

or something along those lines.

Finally, I got a warning about the Normal.dot file since I have Word
configured to automatically save the file when it exits. You might
want to verify that your appWord_DocumentBeforeSave only blocks the
save of the file of interest.


--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top