+ Reply to Thread
Results 1 to 11 of 11

Application won't die!!!

  1. #1
    Registered User
    Join Date
    09-16-2004
    Location
    Virginia
    Posts
    46

    Application won't die!!!

    I create an Excel application object, workbook, and worksheet on the fly in MS Access. When I close everything down, the EXCEL process remains in Task Manager. If I run the code multiple times, I get multiple processes that never die!

    I think I posted my original thread on the wrong forum. Please check it out here and provide any input if you can!!

    http://www.excelforum.com/showthread.php?t=364109

    The full discussion of the problem is at this Access forum:
    http://www.access-programmers.co.uk/...ad.php?t=85282
    Tom Stock
    Office version: MSO 2002 SP3
    OS: Windows XP Pro
    Hardware: IBM Thinkpad T41

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Tom,

    Add these API calls to a VBA Module in your project. This will work like you click the Close Box on the Excel Application.


    API Macro Code:
    _________________________________________________________________

    Option Explicit

    'Declarations
    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

    'Send Message Constant
    Private Const WM_CLOSE = &H10

    Public Sub CloseApp(ByVal Window_Title As String)

    Hwnd = FindWindow(vbNullString, Window_Title)
    SendMessage Hwnd, WM_CLOSE, 0&, 0&

    End Sub

    _________________________________________________________________

    Your Code with CloseApp:

    Private Sub Command0_Click()

    Dim oXL As Excel.Application
    Dim oWb As Excel.Workbook
    Dim oWs As Excel.Worksheet

    Set oXL = New Excel.Application
    oXL.Workbooks.Add
    Set oWb = oXL.Workbooks(Workbooks.Count)
    oWb.Worksheets("Sheet1").Cells(1, 1).Value = "Test"
    oWb.SaveAs "C:\Test.xls"
    oWb.Close
    CloseApp ("Microsoft Excel - Test.xls")

    Set oWb = Nothing
    Set oXL = Nothing

    End Sub

    _________________________________________________________________

    If you have any problems let me know. You can e-mail at [email protected].

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    09-16-2004
    Location
    Virginia
    Posts
    46
    Leith,

    I don't have access to the program at the moment, I'll try it on Monday.

    The only thing I can say upfront is that there is no open window for the application - only an unterminated process. If I look at "Applications" under task manager, nothing is there. Only "EXCEL" appears under "Processes". My gut tells me that there is no title attached to a process, subsequently I don't expect the supplied API calls to work, but who knows? I'll give it a try on Monday!!!

    Thanks for your reply!!

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hi Tom,

    I always test my macros before posting them. This what what I did to check the macro code. I ran the macro and stopped it after it had closed the workbook. I then checked the task manager for another instance of Excel. It was there "hidden" in the tasks. After restarting the macro and letting it execute the API code, I rechecked the tasks and the 'hidden' Excel executable had been terminated. If you have any problems or other questions, e-mail at [email protected].

    Sincerely,
    Leith Ross

  5. #5
    Registered User
    Join Date
    09-16-2004
    Location
    Virginia
    Posts
    46
    Hi Leith,

    I cut and paste your code into a separate module - unfortunately, I'm having the same problem. The Excel process remains despite the application being closed down. This ONLY occurs if I'm closing the application from code. If I run Excel normally and close it down, no problem.

    Any more thoughts?

    Tom

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    HelloTom,

    If you only have Excel running from your code this will work (provided XP Pro hasn't changed Excel's class name). This will close the first instance of Excel found in the system.

    Close Excel by Class:

    Option Explicit

    'Declarations
    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

    'Send Message Constant
    Private Const WM_CLOSE = &H10

    Public Sub CloseApp()

    Hwnd = FindWindow("XLMAIN", vbNullString)
    SendMessage Hwnd, WM_CLOSE, 0&, 0&

    End Sub


    Sincerely,
    Leith Ross

  7. #7
    Registered User
    Join Date
    09-16-2004
    Location
    Virginia
    Posts
    46
    Sorry, still didn't work - the EXCEL process remains..!! Man, this is frustrating! Where did you learn your Win API stuff?

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Tom,

    I am begining to suspect this has to do with system access rights. I know there were quite a few changes made in Windows 2000 and more were added in XP. Unfortunately I don't have access to computer with either system. There are situations were the system will deny an API routine access to a thread or process because of the security rights. These rights might be set through Access. Sorry I don't have a better answer for you.

    To answer your question about the API, books, head banging, and crashing the system. Really isn't much other way to learn it. Helps to know the basics about C code structures before you start.

    Sincerely,
    Leith Ross

  9. #9
    Registered User
    Join Date
    09-16-2004
    Location
    Virginia
    Posts
    46
    That may very well be... we are working in a Managed Environment (although, as a developer, I am semi-managed). They have restricted many things with policy settings - that may be one of the things.

    Thanks again for your help!!!

  10. #10
    Registered User
    Join Date
    09-16-2004
    Location
    Virginia
    Posts
    46

    Update...

    I am working in parallel on some excel VBA functions - I noticed in Excel that when I close a workbook using the wbk.Close method, or even by closing the workbook outright using the close button, the associated VBAProject DOESN'T close with it!!! This may be a source of the problem. Any more thoughts??? Why wouldn't the VBA Project attached to a workbook close with the workbook? The VBA projects only close when I shut down Excel completely!

  11. #11
    Registered User
    Join Date
    09-16-2004
    Location
    Virginia
    Posts
    46

    I think I finally figured it out...

    I think I've finally figured it out. We use LiveLink here at work. It has an associated addin that wasn't allowing the associated VBA projects to close down properly - there was a similar problem with the Google desktop addins. Once I change the appropriate registry key, everything started working properly!

    The registry key in question is:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Excel\Addins\LLAppInt.Connect

    The value that was the problem:
    LoadBehavior (3) <-- load on startup

    Once I changed it to
    LoadBehavior (9) <-- load on deman

    everything started working properly. Yay!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1