473,404 Members | 2,195 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,404 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 4871
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.