DocumentBeforeClose event not always working

K

Kristi

I have a document protected for forms which I will be emailing out. I need
the users to complete one of the fields before closing the document.

Using the Help files, I created a macro to run on the DocumentBeforeClose
event. The Help files stated that you must create a separate Class Module to
post the code in. And it also said to create an event handler macro that
must be run before it will work.

I did these things and the code works after running the event handler macro.
However, do you have to run that macro every time the document is opened?
The code will not execute in subsequent openings of the file, unless the
event handler macro is run again. Isn't there a way to make everything run
smoothly without having to run that macro every time the document is opened?

Here is the code. I have a Class Module called EventClassModule with the
following code (I also have a DocumentOpen event, in the same class module
and having the same issues):

Public WithEvents appWord As Word.Application
-----
Private Sub appWord_DocumentBeforeClose _
(ByVal Doc As Document, _
Cancel As Boolean)

Dim BusJust

If ActiveDocument.FormFields("txt_BusJust").Result = "" Then
MsgBox ("You must complete the" & Chr(10) & "BUSINESS
JUSTIFICATION(S)" & Chr(10) _
& "field before closing this document.")
Cancel = True
ActiveDocument.Bookmarks("txt_BusJust").Select
End If
-----
Private Sub appWord_DocumentOpen(ByVal Doc As Document)
ActiveDocument.ActiveWindow.View.ShowHiddenText = False
End Sub


Then, in the NewMacros module, I have the following code:

Sub Register_Event_Handler()
Set X.appWord = Word.Application
End Sub

Thanks for your help.
Kristi
 
S

Shauna Kelly

Hi Kristi

You're on the right track. You do need to run the code that creates the
event handler whenever the document is opened. You can get Word to do that
automatically for you if you create a macro called AutoOpen and in that
macro call your macro to create the event handler.

See the "Using Auto macros" event in this article for info on the Auto
macros:
http://word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm

This article might help, too:
http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

More specifically, what is X in the following and when does it get created?
Sub Register_Event_Handler()
Set X.appWord = Word.Application
End Sub

At some stage it looks like you need to establish X as an instance of the
EventClassModule class, with something like
Set X = New EventClassModule
and then run Register_Event_Handler.

That's the code that needs to be in your AutoOpen macro.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
K

Kristi

Shauna (or anyone else),

I'm having another problem with this AutoOpen macro, etc. A few things have
changed since I posted the original question, but I thought I should still
keep the question with this thread instead of creating a new one.

I no longer need the DocumentBeforeClose event. So I am really only trying
to make something happen when the document is opened. The document is not
based on a template, it's just a regular document that will be distributed
via email.

The document is protected for Forms. There is a table in the document and
some rows of the table need to be hidden until the user clicks Yes/No on a
corresponding question. I created bookmarks for the hidden rows, hid them
using Font-Hidden, and placed Exit macros on the fields that need to trigger
the rows to appear.

I am testing the doc out with one user and the issue he is having is
inconsistency. When I first email him the document, things work fine. The
hidden fields are hidden as they should be. However, if he saves the
document, all of the hidden fields suddenly appear. Also, when he closes the
document and re-opens it, the hidden fields are all unhidden. So it doesn't
seem as if the macro is ensuring the fields are open EVERY time the document
is opened. Also, it's strange that they appear when the doc is saved, as
stated above.

Any help?
Thanks. Kristi
 
D

Doug Robbins - Word MVP

You are going to run into strife emailing a document that contains macros.
If the recipient has their macro security level set to high, the macro will
be stripped from the document when they open it, without any notification.
If it is set to medium, they will receive a warning message that the
document contains macros and they will be given the opportunity to accept
the document with the macros or to disable them.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Kristi

Thanks, but that's not a problem. We are putting a note to users regarding
setting their security level in the email. So we're not concerned about
that. The user who is testing this doc for me (see other post with problems)
has the macros enabled.
 

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