Determine when OLE server is done

  • Thread starter Victor Boris Arnold
  • Start date
V

Victor Boris Arnold

I am using the acOLECreateEmbed and acOLEActivate actions
to create a Word object for a field in an an Access form.
I'd like to be able to determine in my VBA code when the
user has closed Word. How can this be done?
 
H

haroldk

You could use GetObject and if it raises an error then Word is not opened.
Another option is to use a Windows API function to determine if there is an
instance of Word.

This code sample will print to the immediate Window whether or not an
instance of Word is opened. The only drawback is if the user is using Word
as email editor and Outlook is opened.
Function GetWord() As Boolean
Dim oWrd As Word.Application
Set oWrd = New Word.Application
With oWrd
.Visible = True
.Documents.Add
.Quit
End With
Set oWrd = Nothing
DoEvents
On Error GoTo ErrH
Set oWrd = GetObject(, "Word.application")

GetWord = False
oWrd.Quit
Set oWrd = Nothing
Exit Function

ErrH:
'there is no instance of Word to get so there is an error raised.
GetWord = True

End Function

Sub returnw()
If GetWord = True Then
Debug.Print "Word is Closed"
Else
Debug.Print "Word is Still opened"
End If
End Sub

Harold
 
C

Chad DeMeyer

Or you could use the AppActivate method of the Windows Script Host shell
object, which returns true or false depending on whether it was successful
in activating the target window.

Dim WshShell
<your code here>
Set WshShell = CreateObject("Wscript.Shell")
<more of your code>
If Not WshShell.AppActivate("Microsoft Word") Then
<more of your code>
Set WshShell = Nothing

Regards,
Chad DeMeyer
 

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