Check if Word is already active?

E

Ed

I pulled up an Excel macro I wrote in my absolute newbie days (not that I'm
too much farther down the road!) that opens a Word doc. It still works, but
now I can see that it needs some tweaking. I use
Dim WD As Object
doc = Fpath & "\" & Fname & ".doc"
Set WD = CreateObject("Word.Application")
WD.Documents.Open doc
WD.Visible = True
Set WD = Nothing
(doc, Fpath, and Fname are all declared as well.)
Besides being late binding, this opens an entirely new instance of Word,
which can do some strange things to me, depending on what I'm doing in Word
at the moment. What I'd really like to do is check to see if Word is
running first; if it is, then open the new doc within the current instance -
if not, then open a new instance. What's the best way to check if Word is
already running?

Ed
 
E

Ed

It's not working! And now I have TWO issues!

From the GetObject Help, I gleaned this:
On Error Resume Next
Set WD = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WD = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

I tried this with
Dim WD As Object
and
Dim WD As Word.Application

I stop with error on the GetObject line, which also means Resume Next is not
passing off to the next line. Why??!

The error message is "Active X component can't create object." I do have a
reference to the Word 9 library, and I'm running Word and Excel 2000.

Any suggestions?
Ed
 
H

Helmut Weber

Hi Ed,

try this:

Public Sub Test7777()
' reference to word library set
' = early binding
' tools references

Dim oWrd As Word.Application

On Error Resume Next
Set oWrd = GetObject(, "Word.application")
If Err.Number = 429 Then
Set oWrd = CreateObject("Word.application")
End If
oWrd.Visible = True
Err.Clear

end sub

' --
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Ed

Helmut: I tried both early and late binding. And I used If Err.Number <> 0
rather than the specific error number. It still errors out on the GetObject
line, and Resume Next doesn't pass it down to the next line.

If another instance of Word is already open, the macro runs through fine.
If not, it errors and stops.
Ed
 
H

Helmut Weber

Hi Ed,

hm..., don't know, works perfectly here and now.

Show us all of your code that is relevant,
which is difficult enough to decide. ;-)

Maybe some other helper knows better, please!

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Ed

There's not too much more to it. Remember this is from inside an Excel
macro.

Sub FindDoc()

Dim WD As Object
Dim doc As String
Dim Fname As String
Dim Fpath As String

' Get file path
Fpath = ThisWorkbook.Path

Sheets("Sheet1").Activate

' Get doc number from list page
Fname = ActiveCell.Text

' Open doc
On Error Resume Next
Set WD = GetObject(, "Word.Application")
MsgBox Err.Number
If Err.Number <> 0 Then
Set WD = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

doc = Fpath & "\" & Fname & ".doc"

WD.Documents.Open doc
WD.Visible = True

End Sub
 
H

Helmut Weber

Hi Ed,

I tried this with late binding and it works as well:

Dim WD As Object
On Error Resume Next
Set WD = GetObject(, "Word.Application")
If WD Is Nothing Then
Set WD = CreateObject("Word.Application")
End If
Err.Clear
WD.Visible = True

Maybe I am blind...

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
E

Ed

I don't know, Helmut - it does not work for me. I keep getting the "ActiveX
can't create this" error, and Resume Next won't let it drop through. Is
there an ActiveX library reference I should have checked? I looked, and all
I have referenced is the ActiveX Plugin (Excel 2000).

Ed
 
H

Helmut Weber

Hi Ed,

I have removed all references,
except those two I can't remove, because they are in use.
Visual basic for applications
Microsoft Excel 11 ...

And all is running perfectly.

I don't want to speculate on what could be wrong.
I can't test, or rather I don't want to,
trying with Word as Outlook-Editor,
which might cause problems.

Have you different versions of Word installed?

I'd suggest to start a new thread,
as I can't believe that all the experts
are reading this thread and nobody knows
what's going on.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Ed

I will start a new thread Monday morning (here - US southwest), as my work
week only runs through Thursday and I don't have access to the NG over my
weekends. I do use Word as editor with Outlook - everything ran fine with
OL up, because an instance of Word was active. With OL off, I throw the
error. Also, you have Excel 11 - I have Excel 9 (Word 9 also). I wonder if
that's making a difference. Well, we'll throw this back out next week and
see what happens. Have a good weekend, and thank you very much for your
time!

Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top