Activating Word from within Access

  • Thread starter Thread starter Anita Weiler
  • Start date Start date
A

Anita Weiler

I have successfully created code to start Word and create
a new document from within Access (code is below).
However, if Word is already running, this code starts a
new instance of Word. How can I check if Word is already
running and activate the existing instance?

The code I'm using is:

Dim wordApp As Object
'Start Word and make it the active window
Set wordApp = CreateObject("word.application")
wordApp.Visible = True
wordApp.Activate
wordApp.Documents.Add
 
Dim appWord As Word.Application
Set appWord = GetObject(, "Word.Application") ' get the running
instance
If Err.Number = 429 Or Err.Number = 91 Or Err.Number = 462 Then
Err.Clear
Set appWord = GetObject("", "Word.Application") ' not there, create
a new instance
ElseIf Err.Number > 0 Then
MsgBox Err.Description
Exit Sub
 
The trick is to use GetObject(,"word.appliction") first, if word is NOT
running, then you trap the error, and use CreateObject.

I have nice working example of a template driven word merge system. It
automatically takes your current ms-access form, and adds all the merge
fields to the word document. It also by default merges ONLY the record you
are looking at.

So, basically the whole process of creating a word merge template is
automated for you.

Check out:
http://www.attcanada.net/~kallal.msn/msaccess/msaccess.html
 
Back
Top