Results 1 to 5 of 5

Thread: is there away to close excel apart from xlapp.quit

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 1999
    Location
    Brisbane, QLD, Australia
    Posts
    10
    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.



  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    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.

  3. #3
    New Member
    Join Date
    Nov 2000
    Posts
    9
    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.

  4. #4
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    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.

  5. #5
    Addicted Member eer3's Avatar
    Join Date
    Sep 2000
    Location
    Ca
    Posts
    165
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width