Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

On Exit Macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 25 Oct 2006 16:36 GMT
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.
Tony Jollans - 29 Oct 2006 11:00 GMT
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.

--
Enjoy,
Tony

> 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
[quoted text clipped - 31 lines]
>
> Can anyone explain why this happens?  Thanks.
Helmut Weber - 29 Oct 2006 11:33 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.