472,799 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 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 2640
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jay Xx | last post by:
I have an IFrame in design mode. I've tried a bunch of things to capture key presses in that IFrame, but I can't seem to get it. I can capture key presses outside the IFrame fine. I have this...
4
by: astro | last post by:
I've been looking at the event sinking example in Litwin et. al. "Access 97 Developer's Handbook" and I am able to event sink the 2 events listed - those being the "_quit" and "_documentchange"...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
14
by: Brent Burkart | last post by:
I am trying to capture the Windows Authenticated username, but I want to be able to capture the login name that exists in IIS, not Windows. In order to enter my company's intranet through the...
10
by: Andrew | last post by:
Hi, I have a messagebox that pops up due to an event. I did it in javascript. ie. alert("Time's up. Assessment Ended"); I want to capture the OK and Cancel events of this alert messagebox. My...
5
by: Nick | last post by:
Hey guys, I have 2 events on a windows forms datagrid, the mouse move as well as the double click events. What's happening is that when I double click on a row in the grid, the mouse move event...
1
by: mwhite | last post by:
Hi, I need to find a way of capturing the text from a Word document, and storing it. Preferably as a string, as I then need to be able to manipulate the text in code and display it in my web...
6
by: =?Utf-8?B?Q2h1Y2sgUA==?= | last post by:
I have a Role Provider with a subdirectory protected via the web.config. Forms authentication is used. If a person who is not in the role tries to access a page in the directory, the browser is...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.