Office 2003 BETA 2, Word 11: Can't access ActiveDocument

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
 
W

Word Heretic

G'day "Sam Brow" <[email protected]>,

this is normal behavior for Word, here is what my VBA Spellbook has to
say on the issue for you, it's well worth its purchase price:


The Unholy trinity
This is the "Maggie" equivalent for VBA logic problems, like when Word
goes splat halfway through a long code run. Memorize these functions
for their aid is always magnificent! The unholy trinity are UndoClear,
DoEvents and OnTimer. All three have excellent help in Word's built-in
help system that is not worth repeating here.

DoEvents
DoEvents forces your code to take a back seat so that any background
processes can use some CPU time.
Miscellaneous errors are fixed just through adding in a DoEvents to
force a hand-off to other tasks waiting in the wings. It is especially
useful when updating UserForms, dealing with external applications or
simply letting the pagination engine paginate. So when code asks some
other sub-system to do something and needs the result, follow it up
with a DoEvents.

Undo
Note there is no simple way to access the undo collection, thus it is
difficult to get all your VBA actions to be in undo step. A poor
workaround is to clear the undo collection before your macro so that
you can undo all actions to just undo the macro.
Regardless, you should regularly clear the buffer if you are doing
automated editing using ActiveDocument.UndoClear.

OnTimer
OnTimer delays your code execution for a few seconds. Nothing is
running so Word has a chance to do its things. This is especially
useful during document open where Word runs auto-executing code before
it has quite finished setting up the environment. It particularly
manifests itself in problems like being unable to do stuff to
UserForms and CommandBars on document open. It also fixes sporadic
error 5 conditions on startup.
It works best when called last in the sub routine to prevent long
running code in the first sub-routine from interfering with the pause
effect.

Old Subroutine

Public Sub AutoExec()
'A whole bunch of safe stuff
MyForm.Show 'start of bad stuff block
End Sub

The replacement waits for two seconds before running the offending
code.

Public Sub AutoExec()
'A whole bunch of safe stuff
Application.OnTime When:=Now + TimeValue("00:00:02"), Name:="
DelayedAutoExec"
End Sub

Public Sub DelayedAutoExec()
MyForm.Show 'start of bad stuff block
End Sub



Sam Brow said:
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

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
Email: (e-mail address removed)
Products: http://www.geocities.com/word_heretic/products.html
Spellbooks: 728 pages of dump left and dropping...

The VBA Beginner's Spellbook: For all VBA users.
 

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