473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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,
--

Randy
Aug 22 '05 #1
8 4870
On Mon, 22 Aug 2005 04:52:11 -0700, "Randy Wayne" <ra********@discussions.microsoft.com> wrote:

¤ 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)
Aug 22 '05 #2
Paul:

Thanks for the reply.

Visual Studio states that I need to declare "FindWindow". What variable
type is it?

--
Thanks,

Randy
"Paul Clement" wrote:
On Mon, 22 Aug 2005 04:52:11 -0700, "Randy Wayne" <ra********@discussions.microsoft.com> wrote:

¤ 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)

Aug 22 '05 #3
you should be able to use the diagnostics classes

http://www.c-sharpcorner.com/1/protracker_csv1.asp

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Randy Wayne" <ra********@discussions.microsoft.com> wrote in message
news:38**********************************@microsof t.com...
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,
--

Randy

Aug 22 '05 #4
John:

Thank you for the reply. I will look at that class as well as the link you
provided sometime today.
--
Thanks,

Randy
"John Timney (ASP.NET MVP)" wrote:
you should be able to use the diagnostics classes

http://www.c-sharpcorner.com/1/protracker_csv1.asp

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Randy Wayne" <ra********@discussions.microsoft.com> wrote in message
news:38**********************************@microsof t.com...
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,
--

Randy


Aug 23 '05 #5
On Mon, 22 Aug 2005 12:50:02 -0700, "Randy Wayne" <ra********@discussions.microsoft.com> wrote:

¤ 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)
Aug 23 '05 #6
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

--
Thanks,

Randy
"Paul Clement" wrote:
On Mon, 22 Aug 2005 12:50:02 -0700, "Randy Wayne" <ra********@discussions.microsoft.com> wrote:

¤ 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)

Aug 23 '05 #7
On Tue, 23 Aug 2005 11:46:44 -0700, "Randy Wayne" <ra********@discussions.microsoft.com> wrote:

¤ 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)
Aug 24 '05 #8
Paul:

Fantastic!!

Thank you for sticking with this for me.

Randy
"Paul Clement" wrote:
On Tue, 23 Aug 2005 11:46:44 -0700, "Randy Wayne" <ra********@discussions.microsoft.com> wrote:

¤ 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)

Aug 24 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Otie | last post by:
I found the following under the GetObject help notes and in the example for GetObject: "This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet...
8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
1
by: Gary Cobden | last post by:
Hi I have a routine that uses VBA to open a hidden occurence of Excel, and do background computations. However, in the event that the routine terminates abnormally, I have not been able to...
5
by: Wenke Ji | last post by:
Hi I open a Excel workbook using below API: Set ExcelServer = CreateObject("EXCEL.Application") Set TargetWorkbook = ExcelServer.Workbooks.Open (CurrentBook) Befor the programm exit , I use...
16
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy...
8
by: Randy Wayne | last post by:
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...
5
by: mabond | last post by:
Hi recently read a posting and reply about Excel processs still running after the Appliction.Quit was called. Thought I might be able to use the same...
16
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public...
2
by: hakkatil | last post by:
Hi to all, I have a page that inserts excel sheet to access database. I am using asp. What I want to do is to check the inserting record if it is in the database. Basicly checking the dublicate...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.