ActiveWindow RunTime Error

G

Greg Maxey

I am trying to assist someone remotely isolate a runtime error occurring in
their code. I can't duplicate it on my PC.

They have a procedure that performs the following:

Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="Hdrlogo"
Selection.Range.InsertAutoText
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.TypeBackspace
Selection.GoTo what:=wdGoToBookmark, Name:="bkStop"

Basically it goes to the end of the document, inserts a page break, goes to
the header, inserts some text, converts that text to an AutoText entry, then
returns to a bookmark in the main text.

It works without issue on my PC and several PCs in the users firm. On one
PC the code fails on this line:

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

With the following error message: Run-time error '91:
Object variable or with
block variable not set.

I can not reproduce this error.

Any ideas? Thanks.
 
G

Greg Maxey

Al,

Yes. For reasons to complicated to go into here, I was not able to use that
method. I did learn that my adding ActiveDocument in front of AcitveWindow

ActiveDocument.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader

The problem went away.

Thanks.
 
A

alborg

Hi Greg:

Great job! I betcha you didn't sleep or eat for 2 days to figure that one
out... ;^)

Cheers,
Al
 
G

Greg Maxey

Well I still don't know why it happens ;-)

Sleepless and hungry here in cold western NC.
 
M

Manfred F

Hi Greg,

Greg Maxey said:
Well I still don't know why it happens ;-)
I don't know the reason either, but I've learnt one thing:
using the expressions ActiveDocument, ActiveWindow, and Selection, You get
the context for executing Your macro. The same is true for "Application" and
for some more expressions.
One should do this once and only once during a macro's runtime. If You
repeatedly get the context, what will happen, if the user changes the active
document, window, or selection? Some of Your functionality will be run in a
different context, and You'll probably not like the results.
So, it's a good idea to define a context variable and initialize it in the
beginning:

Dim myDoc as Document
' set context
Set myDoc = ActiveDocument
' use context
myDoc.ActiveWindow.***
myDoc.ActiveWindow.ActivePane.Selection.***

Regards,
Manfred
 
G

Greg Maxey

Thanks Manfred.

Manfred said:
Hi Greg,


I don't know the reason either, but I've learnt one thing:
using the expressions ActiveDocument, ActiveWindow, and Selection,
You get the context for executing Your macro. The same is true for
"Application" and for some more expressions.
One should do this once and only once during a macro's runtime. If You
repeatedly get the context, what will happen, if the user changes the
active document, window, or selection? Some of Your functionality
will be run in a different context, and You'll probably not like the
results.
So, it's a good idea to define a context variable and initialize it
in the beginning:

Dim myDoc as Document
' set context
Set myDoc = ActiveDocument
' use context
myDoc.ActiveWindow.***
myDoc.ActiveWindow.ActivePane.Selection.***

Regards,
Manfred
 

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