On Exit Macro

G

Greg Maxey

I discussed this issue in a very long post yesterday as a sidebar and
thought it better to simplify my observations and question here as a
dedicated post. I will sacrifice my second born child as repentence
for double posting.

This code works as expected:

Sub Test()
Dim oDoc As Word.Document
Dim oRng As Word.Range
Set oDoc = Documents.Add
Set oRng = oDoc.Range
oRng.Text = "Test"
MsgBox oDoc.Range
oDoc.Close wdDoNotSaveChanges
End Sub

I can call the code above using this code and it still works as
expected:

Sub OnExitFormField()
Test
End Sub

However, if I assign OnExitFormField () to the exit event of a
protected formfield then when I tab out of that formfield a Run-Time
Error 4198 "Command Failed" occurs on this line:

oDoc.Close wdDoNotSaveChanges

But, if I put a break in the code at that line and then tab out of the
field the editor runs to that line and stops and I can manually step
through the remainder of the code without error.

Can anyone explain why this happens? Thanks.
 
T

Tony Jollans

Hi Greg,

Nobody else has bitten on this one so I'll have a say although I can't
really answer your question.

There are some actions - perhaps limited to file I/O - which seem to have
some prerequisite actions that cannot be done from VBA, or allowed to happen
while VBA code is running, but which happen automatically when break mode is
established. Word is in some kind of state where it cannot perform the
action and that state is freed only when code is stopped. Does that all make
sense?

You can see the same behaviour if you put some DoEvents in AutoExec macros
and then double click on a document to open it. The DoEvents allows the
Document to (try to) open before Word is ready and you get a 'cannot find
file' message. If you add a msgbox to the macro and then press Ctrl+Break
and then Debug and F5 the document opens and the automacro runs to
completion without error - the break mode having been enough to clear the
hurdle (whatever it is).

I would love to understand this but all I can manage at the moment is to
observe, and be aware.
 
H

Helmut Weber

Hi everybody,

I blame problems like that on multitasking,
that is to say, that a process starts before
the preceeding process was completed,
in a very simplified manner.

I've tried to arrange commandbars in a document
to be opened in autoopen or autoexec or autonew.
No way. An ontime macro was required.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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