Hi Folks

I've been trying to solve the following problem on & off for about 6 weeks.

If anyone can shed any light on it, I'd be very appreciative!

I'm trying to print a document from MS Access 97 VBA using automation to MS
Word 97. I'm assuming the problem is Windows/VBA related, not Access
related, and would therefore happen in VB also. Hence my desperate resort
to this newsgroup!

Here's the VBA I'm using:

Public Sub doit()
Dim oWord As Object
Set oWord = CreateObject("word.application")
oWord.Documents.Open "C:\TEST.doc"
'MsgBox "BLAH" ' [A]
'oWord.Visible = True ' [B]
oWord.PrintOut Background:=True
' [C]
MsgBox "OK"
oWord.Quit 0 ' 0=discard changes.
Set oWord = Nothing
End Sub

The document in question has a margin set outside the printable area of the
page. This can be achieved by opening any document, going to page preview
mode, dragging the top margin right up close to the top of the page, then
saving the document & exitting Word. Printing the document (in the normal
way from Word) should display a dialog box: "The margins of section 1 are
set outside the printable area of the page. Do you want to continue?
[Yes]/[No]".

When I run this sub from the Access IDE, the dialog box noted above is
displayed BELOW the Access IDE not ABOVE it so it is not visible - so
the user does not know it's there, and can not answer it. The sub then
appears to hang. Any reference in the sub to any method or property of the
oWord object, will hang (eg. oWord.Quit). Even trying to set
oWord.Visible=True will hang.

I concluded that I needed, on return from the .PrintOut call, to detect
whether Word had displayed a dialog box, and if so, bring it to the
foreground (using API calls, not oWord methods or properties) so the user
could answer it.

I confirmed that the oWord.PrintOut statement returns immediately to point
[C] before the dialog box has been displayed. I wrote some code to wait for
a second or two at that point, then look for the dialog box with
FindWindow("#32770","Microsoft Word"). This code successfully found (&
returned the handle to) the relevant dialog box.

However, I can not then (for the life of me) swap to the dialog box in such
a way that everything continues ok when the user answers it! I've tried:
BringWindowToTop on every conceivable window handle including the Word main
window handle, the dialog box window handle, the dialog box OK button
window handle, and so on;
SetForeGroundWindow ditto;
Access VBA AppActivate on the Word window caption, and
Many & varied combinations of the above!

Whatever I do, the following happens:
My code makes the dialog box appear (with or without the Word main window);
The dialog box lets me click Yes or No;
Someone (presumably Word or Windows) repaints the screen area that the
dialog box overlayed, but then:
MS Word hangs! Its window is still displayed, but it doesn't respond to any
keystrokes, mouse clicks, dragging actions or whatever.
Then the Access IDE hangs: sometimes showing a popup message that MS Word
is not responding, and sometimes not.

And here's the rub! :-

If I include statement [A] or [B], it all works fine! (ie. nothing hangs).
If I don't include them, it hangs as described above!

I've checked GetActiveWindow() at various points (with and without
statements [A] and [B]) but this always points to the Access IDE window.

This is driving me A*B*S*O*L*U*T*E*L*Y M*E*N*T*A*L.

Can anyone help me on this please.

Thanks,
TC