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

Re: DocumentBeforeClose event not always working by Shauna

Shauna
Wed Oct 24 02:29:13 PDT 2007

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


"Kristi" <Kristi@discussions.microsoft.com> wrote in message
news:53E9D140-D173-4F24-98F9-06E4BB0F7515@microsoft.com...
>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



Re: DocumentBeforeClose event not always working by Kristi

Kristi
Wed Oct 24 08:25:02 PDT 2007

Thanks, Shauna! I think it's working now.

Kristi

Re: DocumentBeforeClose event not always working by Kristi

Kristi
Sun Oct 28 12:42:00 PDT 2007

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



Re: DocumentBeforeClose event not always working by Doug

Doug
Sun Oct 28 23:33:13 PDT 2007

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

"Kristi" <Kristi@discussions.microsoft.com> wrote in message
news:54727D23-B5FE-44E8-8856-46ABD0729D63@microsoft.com...
> 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
>
>



Re: DocumentBeforeClose event not always working by Kristi

Kristi
Mon Oct 29 07:11:01 PDT 2007

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.

"Doug Robbins - Word MVP" wrote:

> 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
>
> "Kristi" <Kristi@discussions.microsoft.com> wrote in message
> news:54727D23-B5FE-44E8-8856-46ABD0729D63@microsoft.com...
> > 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
> >
> >
>
>
>