Connecting Tech Pros Worldwide Forums | Help | Site Map

Capturing Word Events from Access

Fly Girl
Guest
 
Posts: n/a
#1: Nov 12 '05
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!

rkc
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Capturing Word Events from Access


This answer comes strictly from reading the Word VBA help file.
Never done this myself.
The added code is inline below.

"Fly Girl" <gschipper@rgl.net> wrote in message
news:446785ab.0312031407.6578905b@posting.google.c om...[color=blue]
> 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[/color]

<add this maybe>
Set myWord.App = Word.Application
</add this maybe>
[color=blue]
> strFile = frm![EventsSub].Form.txtLinkFilePath
>
> myWord.OpenNewDocument (strFile)
>
> Etc....[/color]



rkc
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Capturing Word Events from Access


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" <rkc@yabba.dabba.do.rochester.rr.bomb> wrote in message
news:f9xzb.184469$ZC4.26827@twister.nyroc.rr.com.. .[color=blue]
> This answer comes strictly from reading the Word VBA help file.
> Never done this myself.
> The added code is inline below.[/color]



Gail Schipper
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Capturing Word Events from Access


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!
Gail Schipper
Guest
 
Posts: n/a
#5: Nov 12 '05

re: Capturing Word Events from Access


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!
rkc
Guest
 
Posts: n/a
#6: Nov 12 '05

re: Capturing Word Events from Access



"Gail Schipper" <gschipper@rgl.net> wrote in message
news:3fcf533a$0$88382$75868355@news.frii.net...[color=blue]
> 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.[/color]

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>







Closed Thread


Similar Microsoft Access / VBA bytes