Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test if MS Word is open - how? 2

Status
Not open for further replies.

MrPeds

Programmer
Jan 7, 2003
219
0
0
GB
Hi,

I'd like to know if there is any way for my VB application to test if MS word is currently open - my code carries out some calculations that use word docs.

I'd basically like a function like:

Function isWordOpen() As Boolean

'if word is open return true, else return false.

End Function

Thanks,

MrPeds
 
Hi MrPeds

Here's the code to your question:

Function IsWordOpen() As Boolean
On Error Resume Next
Dim appWord As Object
Set appWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
IsWordOpen = False
ElseIf Err.Number <> 0 Then
IsWordOpen = False
MsgBox &quot;GetObject Error&quot;
Else
IsWordOpen = True
Set appWord = Nothing
End If
On Error GoTo 0
End Function

Regards
Philipp
 
Another possible solution.

Private Declare Function FindWindow Lib &quot;user32&quot; Alias &quot;FindWindowA&quot; (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Function IsWordOpen() As Boolean
IsWordOpen = FindWindow(&quot;OpusApp&quot;, vbNullString)
End Function

Although it works, but I would suggest pvw's method. Because that is more smart.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top