Checking to see if Excel.exe is running - then close it

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I use a program that imports into Excel that requires that the workbook is
closed prior to the import.

How do you check to see if an Excel workbook is open, and then close the
workbook?

How do you then look to see if Excel is still running and then close it
(Excel.exe)?

I already know how to open an instance of Excel, use it, and then close it,
but I have not figured out how to check for an already open instance of a
workbook and/or Excel.

Thanks,
 
¤ I use a program that imports into Excel that requires that the workbook is
¤ closed prior to the import.
¤
¤ How do you check to see if an Excel workbook is open, and then close the
¤ workbook?
¤
¤ How do you then look to see if Excel is still running and then close it
¤ (Excel.exe)?
¤
¤ I already know how to open an instance of Excel, use it, and then close it,
¤ but I have not figured out how to check for an already open instance of a
¤ workbook and/or Excel.

You can use API function calls to do this reliably. You can also use the second argument in
FindWindow, which is the window (title) name.

Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg
As Int32, _
ByVal wParam As Int32, ByVal
lParam As Int32) As Int32

Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As
String, _
ByVal lpDirectory As
String, _
ByVal lpResult As
System.Text.StringBuilder) As Int32

Public Function TerminateExcel()

Dim ClassName As String
Dim WindowHandle As Int32
Dim ReturnVal As Int32
Const WM_QUIT = &H12

Do

ClassName = "XLMain"
WindowHandle = FindWindow(ClassName, Nothing)

If WindowHandle Then
ReturnVal = PostMessage(WindowHandle, WM_QUIT, 0, 0)
End If

Loop Until WindowHandle = 0

End Function


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Paul:

Thanks for the reply.

Visual Studio states that I need to declare "FindWindow". What variable
type is it?
 
¤ Paul:
¤
¤ Thanks for the reply.
¤
¤ Visual Studio states that I need to declare "FindWindow". What variable
¤ type is it?

FindWindow is an API function call. It should be declared at the top of the code module. The declare
is in the code I posted.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Paul:

Again I thank you for the reply.

Please forgive my ingorance, but I have looked and looked for a Declaration
for "FindWindow". I have copied back the two Declare statements from your
first post.

What am I missing?

Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd
As Int32, _
ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As
Int32

Declare Function FindExecutable Lib "shell32.dll" Alias
"FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory As String, _
ByVal lpResult As System.Text.StringBuilder) As Int32
 
¤ Paul:
¤
¤ Again I thank you for the reply.
¤
¤ Please forgive my ingorance, but I have looked and looked for a Declaration
¤ for "FindWindow". I have copied back the two Declare statements from your
¤ first post.
¤
¤ What am I missing?
¤
¤ Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd
¤ As Int32, _
¤ ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As
¤ Int32
¤
¤ Declare Function FindExecutable Lib "shell32.dll" Alias
¤ "FindExecutableA" _
¤ (ByVal lpFile As String, ByVal lpDirectory As String, _
¤ ByVal lpResult As System.Text.StringBuilder) As Int32

Sorry, my bad. I posted the incorrect API declare. Below is FindWindow:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal
lpWindowName As String) As Int32



Paul
~~~~
Microsoft MVP (Visual Basic)
 
Back
Top