-
Hi,
I know ther is a problem with closing excel from withing applications.
I have tried and crashed and burnt to feel really frustrated now.
Is there a way to close the excel through code apart from using the
xlapp.quit
set xlapp = nothing
I will have to locate the excel window using code and then how do I close that window.
:confused:
-
This should work with Excel 2000, it probably also works with previous versions though.
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Sub cmdCloseExcel_Click()
Dim hWndExcel As Long
hWndExcel = FindWindow("XLMAIN", vbNullString)
If hWndExcel Then Call SendMessage(hWndExcel, WM_CLOSE, 0, ByVal 0)
End Sub
Untested, but works in theory. :rolleyes:
-
Yonatan, I tried the method of using the functions "FindWindowA" and "SendMessageA." However, nothing seemed to happen. Excel is still listed in the running programs window and nothing else seemed to be closed in its place. I am using Excel 97 rather than 2000.
Any other ideas you might have would be helpful.
-
You might have noticed that I used the string XLMAIN as the first parameter of FindWindow.
This is because XLMAIN is Excel 2000's classname.
I don't have Excel 97 anymore, so I can't tell you the classname. But I can tell you how I found it:
There is a little utility that comes with Visual Studio called Spy++. This will tell you the window names and classnames of all the windows currently open.
Find Excel 97 in the list, and the classname will be next to it.
For example, to find the classname of Excel 2000, I looked through the list until I found this:
000009F8 "Microsoft Excel - Book1" XLMAIN
The hexadecimal number in the beginning is the current hWnd of Excel. It shouldn't matter, since this is different every time.
The first string is the current title of Excel.
The second string, what you are looking for, is the classname.
-
Hey all,
I posted a reply on exiting from excel on this thread, have a look, it may help
http://forums.vb-world.net/showthrea...threadid=68080