471,089 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

Capturing Word Events from Access

Gurus needed! I'm trying to use a Word class object in order to
capture events in Word. Here's what I have, but the event code
appWord_BeforeDocumentSave... doesn't ever fire.

In a Class Module ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++

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

Public 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

Public Sub OpenNewDocument(strFileName As String)
Set docWord = appWord.Documents.Open(strFileName)
End Sub

Public Sub Class_Initialize()
Set appWord = New Word.Application
appWord.Visible = True
End Sub

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++

Then in my regular code module
+++++++++++++++++++++++++++++++++++++++++++++++

Public Function OpenFile()
On Error GoTo Err_OpenFile

Dim frm As Form
Dim myWord As clsMSWord
Dim strFile As String

Set frm = Screen.ActiveForm

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

Set myWord = New clsMSWord
strFile = frm![EventsSub].Form.txtLinkFilePath

myWord.OpenNewDocument (strFile)

Etc....
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++

This creates an instance of the class and opens Word and the file.
However, I cannot get the _BeforeDocumentSave event to work.

Am I completely delusional or should there be a way to do this?

Thanks!
Nov 12 '05 #1
5 2533
rkc
This answer comes strictly from reading the Word VBA help file.
Never done this myself.
The added code is inline below.

"Fly Girl" <gs*******@rgl.net> wrote in message
news:44**************************@posting.google.c om...
Then in my regular code module
+++++++++++++++++++++++++++++++++++++++++++++++

Public Function OpenFile()
On Error GoTo Err_OpenFile

Dim frm As Form
Dim myWord As clsMSWord
Dim strFile As String

Set frm = Screen.ActiveForm

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

Set myWord = New clsMSWord
<add this maybe>
Set myWord.App = Word.Application
</add this maybe>
strFile = frm![EventsSub].Form.txtLinkFilePath

myWord.OpenNewDocument (strFile)

Etc....


Nov 12 '05 #2
rkc
Fiddled with this for a bit an actually got it working, but when
run from Access the msgbox does not pop up on top of
the open document. I got an hourglass cursor and had to set
focus back to Access to see the msgbox.
"rkc" <rk*@yabba.dabba.do.rochester.rr.bomb> wrote in message
news:f9********************@twister.nyroc.rr.com.. .
This answer comes strictly from reading the Word VBA help file.
Never done this myself.
The added code is inline below.


Nov 12 '05 #3
What? READ the Help files. How shocking.

Actually I did that several times and what I evetually ended up with is
below. I instantiated the class object in the class module because when
I tried it in the code module I kept getting "Active X can't create
object". The class module Sub Class_Initialize runs whenever I assign a
variable to a new class object.

However, I don't even get the hourglass--you lucky devil, getting that
hourglass. Nothing, but nothing happens when I click Save on the open
Word .doc. I keep seeing posts from people who seem to have accomplished
this feat, though.

Code in Class Module:

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

Public 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

Public Sub OpenNewDocument(strFileName As String)
Set docWord = appWord.Documents.Open(strFileName)
End Sub

Public Sub Class_Initialize()
Set appWord = New Word.Application
appWord.Visible = True
End Sub
Code in regular Module:

Public Function OpenFile()
On Error GoTo Err_OpenFile

Dim frm As Form
Dim bolWordOpen As Boolean
Dim myWord As clsMSWord
Dim strFile As String

Set frm = Screen.ActiveForm

If frm![EventsSub].Form!FileType = "doc" Then
Set myWord = New clsMSWord

strFile = frm![EventsSub].Form.txtLinkFilePath

myWord.OpenNewDocument (strFile)

Etc....

At this point the doc is open but clicking on Save, SaveAs etc. doesn't
trigger the event routine in the Class Module.

I'm not sure if I need a magic wand or a bigger stick.

Thanks for your input, though!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #4
Whohoo! I got it to work and it's quite spiff! Peter Huang on one of the
MSDN boards showed me the error of my ways. The reason I wasn't getting
any event action was because I declared the class object within the
function instead of as a module-level variable. This should have been
obvious, but, hey, sometimes you just can't see the forest for all those
dang trees!

To get the message box to be visible I had to minimize Word for a
moment. In any case, my users can now look at, print preview, print,
etc. but they can't make changes.

Here's the glorious code:

Class Module-----------------------------------------

Option Compare Database
Option Explicit

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

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

docWord.Close False
appWord.Quit

End Sub

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

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

End Sub

Public Sub OpenNewDocument(strFileName As String)
Set docWord = appWord.Documents.Open(strFileName)
End Sub

Public Sub Class_Initialize()
Set appWord = New Word.Application
appWord.Visible = True
End Sub

----------------------------------------------------------

Code Module-----------------------------------------------

Dim myWord As clsMSWord

Public Function OpenFile()
On Error GoTo Err_OpenFile

Dim frm As Form
Dim bolWordOpen As Boolean
Dim strFile As String

Set frm = Screen.ActiveForm

If IsNull(frm![EventsSub].Form.oleLinked) Then
MsgBox "There is no file attached to this event.", vbOKOnly,
"Attachment Error"
Exit Function
End If

' Word files should be opened by Word so we can manipulate them
appropriately
If frm![EventsSub].Form!FileType = "doc" Then

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

' Open as class object so events can be controlled
Set myWord = New clsMSWord

' Opening the file errors if I pass it the file name without
assigning it to a variable
strFile = frm![EventsSub].Form.txtLinkFilePath

myWord.OpenNewDocument (strFile)

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

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

-----------------------------------------------------------

Hope this saves someone else some time!
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #5
rkc

"Gail Schipper" <gs*******@rgl.net> wrote in message
news:3f***********************@news.frii.net...
What? READ the Help files. How shocking.

Actually I did that several times and what I evetually ended up with is
below. I instantiated the class object in the class module because when
I tried it in the code module I kept getting "Active X can't create
object". The class module Sub Class_Initialize runs whenever I assign a
variable to a new class object.

However, I don't even get the hourglass--you lucky devil, getting that
hourglass. Nothing, but nothing happens when I click Save on the open
Word .doc. I keep seeing posts from people who seem to have accomplished
this feat, though.


Here's the code I used with your class module.
Result: Document opened
File-Save fired the event in your class module
Cursor changed to hourglass
Clicking Access application in the taskbar showed the msgbox
Dismissing the msgbox set focus to the original calling form
I posted again and went back to sleep.

<form module>
Option Compare Database
Option Explicit

Dim myword As clsMSWord

Public Function OpenFile()
Set myword = New clsMSWord
RegisterEventHandler
myword.OpenNewDocument ("d:\newWord.doc")
End Function

Sub RegisterEventHandler()
Set myword.appWord = Word.Application
End Sub

Private Sub Command0_Click()
Call OpenFile
End Sub
</form module>



Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Brent Burkart | last post: by
10 posts views Thread by Andrew | last post: by
1 post views Thread by mwhite | last post: by
6 posts views Thread by =?Utf-8?B?Q2h1Y2sgUA==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.