User Automation to close Word Document and return focus to Access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My Access database controls a Mail Merge Job in Word. After generating the
data, Access opens the Word document with these lines of code in a module:

Set objWord = GetObject("G:\Access Projects\MyWordDoc.doc", "Word.Document")
objWord.Application.Visible = True
objWord.RunAutoMacro wdAutoOpen

In the Word document the Sub AutoOpen Prints the mail merge job. My AutoOpen
code ends with ActiveDocument.Close wdDoNotSaveChanges. Unfortunately, I am
left looking at Word with no open document. I want control returned to the
Access database form where I was before. Suggestions appreciated. Someone in
the Word Programming Community referred me here. Thank you.
 
Hi Richard,

You could just add
Application.Quit
(I think that's what Word needs) after the
ActiveDocument.Close wdDoNotSaveChanges
but this causes problems if Word was already running when Access
executed
set objWord = GetObject("filename.doc")
.. This is because GetObject will grab an existing instance of Word if
there is one, so
Application.Quit
will upset the user by trying to close their instance of Word.

So a better approach is probably to do something like this in the Word
macro:

ActiveDocument.Close wdDoNotSaveChanges
If Application.Documents.Count = 0 Then
'There are no other documents open so we can
'safely close this instance of Word
Application.Quit
Else
'Word was in use before we opened this document
'so we'd better not close it
'but we can maybe activate Access
AppActivate "Microsoft Office Access", False
End If

See Help for AppActivate to work out what its arguments need to be.
 
Hello John,

Adding Application.Quit after ActiveDocument.Close wdDoNotSaveChanges did
not quit the instance of Word. I wonder if it is because my AutoOpen
procedure resides in a module attached to the document, so "Quit" can't be
read after I close the document. This may also be why your suggested
AppActivate "Microsoft Office Access", False did nothing. Here is what did
work, and returns control to Access.

If Application.Documents.Count = 1 Then
'Only my document is open so I can
'safely close this instance of Word.
Application.Quit wdDoNotSaveChanges
Else
ActiveDocument.Close wdDoNotSaveChanges
'Just close the active document.
End If

Thank you for your help.
 
Yup. That makes sense. Thanks for the feedback.

Hello John,

Adding Application.Quit after ActiveDocument.Close wdDoNotSaveChanges did
not quit the instance of Word. I wonder if it is because my AutoOpen
procedure resides in a module attached to the document, so "Quit" can't be
read after I close the document. This may also be why your suggested
AppActivate "Microsoft Office Access", False did nothing. Here is what did
work, and returns control to Access.

If Application.Documents.Count = 1 Then
'Only my document is open so I can
'safely close this instance of Word.
Application.Quit wdDoNotSaveChanges
Else
ActiveDocument.Close wdDoNotSaveChanges
'Just close the active document.
End If

Thank you for your help.
 
Back
Top