S
Sam Brow
This was originally posted in
microsoft.public.word.vba.addins but I'm reposting it here
because the "vba.addins" groups isn't accessible from the
communities.microsoft.com/newsgroups site.
I am running Word 2003 beta 2 (with the additional update).
I have a global template that resides in OFFICE11\STARTUP
and is digitally signed (using selfcert.exe) and set to
be "Trusted". I have macro security set to High and
the "Trust all installed add-ins and templates" option
unchecked. This is security settings that Microsoft seems
to be recommending for end-users so I want to ensure my
code runs in this environment.
I'm using the standard technique where I have a Class
Module declaring a "with events" variable of type
Application. The AutoExec() method creates an object of
this type and sets up the variable so my App_DocumentChange
() event will get called.
I find that the very first time my App_DocumentChange()
method gets called (at Word startup time), I am unable to
access ActiveDocument *IF* the "Trust all installed add-
ins and templates" setting is unchecked. Word silently
ends the macro at the line that references ActiveDocument
and no futher event handlers are called in that template.
Strangely, I *can* access ActiveDocument on the second
call to App_DocumentChange().
In the case where I can't access ActiveDocument, I also
get the same behavior trying to access
Application.Documents.Item(1) but I *can* access
Application.Documents.Count with no problem.
In case it helps, here's a simplified reproducible
example. Here is my "ThisDocument" module...
Option Explicit
Dim g_Events As New Events
Sub AutoExec()
Set g_Events.App = Word.Application
End Sub
and here is my Events class module...
Option Explicit
Public WithEvents App As Word.Application
Private Sub App_DocumentChange()
Dim myDoc As Document
MsgBox ("In App_DocumentChange")
If Word.Documents.Count >= 1 Then
MsgBox ("before ActiveDocument")
Set myDoc = Word.Application.ActiveDocument
MsgBox ("after ActiveDocument")
End If
MsgBox ("Out App_DocumentChange")
End Sub
When this runs (at startup), the first two MsgBox calls
appear but the second two do not. Adding all forms of "On
Error" do not change the behavior.
This behavior is bad because it kills the whole solution
(no more events fire).
Can somebody please try this to confirm the behavior I am
seeing in Word 2003 Beta 2? (I've confirmed it on two
separate machines at my site)
I've run this on Word XP and it does NOT exhibit this
problem (so I'm hoping it is a bug in the beta and will be
fixed before final release).
If your app uses App_DocumentChange() and you expect
customers to start running with the "Trust all installed
add-ins and templates" option unchecked, you should be
aware of this issue.
Thanks
-Sam
microsoft.public.word.vba.addins but I'm reposting it here
because the "vba.addins" groups isn't accessible from the
communities.microsoft.com/newsgroups site.
I am running Word 2003 beta 2 (with the additional update).
I have a global template that resides in OFFICE11\STARTUP
and is digitally signed (using selfcert.exe) and set to
be "Trusted". I have macro security set to High and
the "Trust all installed add-ins and templates" option
unchecked. This is security settings that Microsoft seems
to be recommending for end-users so I want to ensure my
code runs in this environment.
I'm using the standard technique where I have a Class
Module declaring a "with events" variable of type
Application. The AutoExec() method creates an object of
this type and sets up the variable so my App_DocumentChange
() event will get called.
I find that the very first time my App_DocumentChange()
method gets called (at Word startup time), I am unable to
access ActiveDocument *IF* the "Trust all installed add-
ins and templates" setting is unchecked. Word silently
ends the macro at the line that references ActiveDocument
and no futher event handlers are called in that template.
Strangely, I *can* access ActiveDocument on the second
call to App_DocumentChange().
In the case where I can't access ActiveDocument, I also
get the same behavior trying to access
Application.Documents.Item(1) but I *can* access
Application.Documents.Count with no problem.
In case it helps, here's a simplified reproducible
example. Here is my "ThisDocument" module...
Option Explicit
Dim g_Events As New Events
Sub AutoExec()
Set g_Events.App = Word.Application
End Sub
and here is my Events class module...
Option Explicit
Public WithEvents App As Word.Application
Private Sub App_DocumentChange()
Dim myDoc As Document
MsgBox ("In App_DocumentChange")
If Word.Documents.Count >= 1 Then
MsgBox ("before ActiveDocument")
Set myDoc = Word.Application.ActiveDocument
MsgBox ("after ActiveDocument")
End If
MsgBox ("Out App_DocumentChange")
End Sub
When this runs (at startup), the first two MsgBox calls
appear but the second two do not. Adding all forms of "On
Error" do not change the behavior.
This behavior is bad because it kills the whole solution
(no more events fire).
Can somebody please try this to confirm the behavior I am
seeing in Word 2003 Beta 2? (I've confirmed it on two
separate machines at my site)
I've run this on Word XP and it does NOT exhibit this
problem (so I'm hoping it is a bug in the beta and will be
fixed before final release).
If your app uses App_DocumentChange() and you expect
customers to start running with the "Trust all installed
add-ins and templates" option unchecked, you should be
aware of this issue.
Thanks
-Sam