checking for MS Word

  • Thread starter Thread starter Meryl
  • Start date Start date
M

Meryl

I have a macro that opens Word and populates a letter
with fields from my Access form. However, each time the
macro is invoked it opens another occurrence of Word.
How can I add code to check to see if Word is already
opened?

Thx.
Meryl
 
Hi Meryl,

As far as I know you can only do this in VBA, not in a macro. Here's how
I usually do it. This first bit will grab hold of an existing instance
of Word or else launch a new one:

Dim oWord As Word.Application
Dim blWordAllMine As Boolean

On Error Resume Next 'handle errors inline
'Get hold of Word if it's running
Set oWord = GetObject(, "Word.Application")
If oWord Is Nothing Then 'need to fire it up ourselves
Set oWord = CreateObject("Word.Application")
If oWord Is Nothing Then 'Failed
MsgBox "Sorry, could not launch Word," & MSG_NEEDED, _
vbOKOnly + vbExclamation, MSG_TITLE
Exit Sub
Else
blWordAllMine = True
End If
Else
blWordAllMine = False
End If
On Error GoTo 0 'reset error handling

Then when your code has finished with Word, you need to release the
Word.Application object without disturbing an already-running instance
of Word, something like this:

If blWordAllMine Then
oWord.Quit False
Else
'close any documents your code opened
...
'set the corresponding document objects to Nothing
...
End If
Set oWord = Nothing
 
"Meryl" said:
I have a macro that opens Word and populates a letter
with fields from my Access form. However, each time the
macro is invoked it opens another occurrence of Word.
How can I add code to check to see if Word is already
opened?

Thx.
Meryl

Meryl

Have a look at this page on The Access Web, which shows you how to check if an
Application is already running - http://www.mvps.org/access/api/api0007.htm
 
Back
Top