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!

Closing Excel

Status
Not open for further replies.

woodyinoz

IS-IT--Management
Jan 8, 2002
215
GB
Hi all,

I'm looking for aome code that will close Excel.

Can anyone help me out?

Thanks,

Woody.
 
Woody,

There are a few different ways you can do this, but this one is one of the most flexible that I've found:

Code:
method pushButton(var eventInfo Event)
var
   dstr  DynArray[] String
   str   String
   li    LongInt
endVar

const
   WM_CLOSE = 16;
endConst

   enumWindowHandles( dstr, "XLMAIN" )
   dstr.View()
   forEach str in dstr
      li = longInt( str )
      winPostMessage( li, WM_CLOSE, 0, 0 )
   endForEach

endMethod

It works by sending a message to all open Excel windows; the message is similar to the one that Windows itself sends to applications when telling them to close.

Now, this is based on Excel 2002 (I don't have MSO 2002), however, it should work similarly.

If it doesn't work, then it's possible that MS changes the name of the window class for the main Excel window. If they did, you can determine the name of your Excel window class with a bit of investigation--which is precisely what I did to build this snippet. Here's the basic process:

1. Fire up Excel and Paradox. Shut down any extraneous processes you can.

2. Create a dummy form, drop a button on it, and then add the following code to its pushButton() event:

Code:
var
   tv  TableView
endVar

   enumWindowNames( ":priv:winnames" )
   tv.open( ":priv:winnames" )

3. Save your form, run it, and then click the button. You'll see a table showing the names of all applications currently running on Windows.

4. Search the WindowName field for "..excel.." (no quotes) and then note the value in the ClassName field. That's the value you need to pass to the enumWindowHandles() call in the above code.

Now, there are several ways you can approach this problem. I like this one for it uses the oldest--and most widely supported--technology in Windows for communication between applications, e.g. messages. It also works in nearly all versions of Windows, including older 16-bit versions.

Also, you don't really need to name your constant WM_CLOSE; however, I chose it because that's the name of the message in the Windows SDK and I feel it's important to reuse names as far as possible. (It makes your code clearer to other programmers who may deal with t down the pike.)

Hope this helps...

-- Lance
 
Whoops...

You can pull out the
Code:
dstr.View()
line if you like. I added that for debugging purposes while I was testing the code.

Sorry...

-- Lance

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top