Link to home
Start Free TrialLog in
Avatar of Cut_OS
Cut_OS

asked on

Excel process isn't closing

There is a Visual Basic Form App created as exe-File. This app is using Excel to do some stuff. This Code is closing the excel-process regular:

    Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application

    Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

    Private Function AuswertungsTool_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ... some stuff ...
            aFunction(sender, e)
            xlApp.Quit()
            xlApp = Nothing
            End
        End If
    End Function

Open in new window


Now i want return a status-code by using the ExitProcess Call:
    Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application

    Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

    Private Function AuswertungsTool_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ... some stuff ...
            aFunction(sender, e)
            xlApp.Quit()
            xlApp = Nothing
            Call ExitProcess(2)
        End If
    End Function

Open in new window

After the call ExitProcess() the excel-process isn't closing and hanging in the task manager (i thought it's the job of xlApp.Quit()). It seems to be a different between "End" and "ExitProcess()".

Have anybody an idea?

If i knew the PID of the excel.exe i could close the process manually...

Thank you in advance!

Avatar of dlmille
dlmille
Flag of United States of America image

The Windows API can provide PID of current Excel process:

Declare Function GetCurrentProcessId Lib "kernel32" () As Long  - in appropriate VB.NET parlance...

See:  http://www.informit.com/articles/article.aspx?p=366892&seqNum=3

Dave
If you did this would you get the result you desired?

 
Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application

    Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

    Shared Function AuswertungsTool_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ... some stuff ...
            aFunction(sender, e)
            xlApp.Quit()
            xlApp = Nothing
            Return 2
            'Call ExitProcess(2)
        End If
    End Function

Open in new window

Not sure why I changed the scope of the function from Private to Shared (coffee anyone? :)

At any rate, I think while VB6 leveraged ExitProcess as a method for returning values, you need to use Return for that in VB.NET.  At least, from the references I just checked, it appears that's the case.  see:  http://www.pcreview.co.uk/forums/equivalent-vb6-exitprocess-vbulletin-net-t1319903.html

Cheers,

Dave
Avatar of Cut_OS
Cut_OS

ASKER

Thank you for your ideas Dave!

If I'm using return (int) the vb-form is visible and waiting for users action. But the exe shall be invisible and working in the background, when called with arguments. Called without arguments the exe accept user's input, like textfields and buttons - this is working fine.

If i use return and debug i can't see a code line! Is the next line in the main() routine? If yes, why i can't debug main()?

Holger


Avatar of Cut_OS

ASKER

GetCurrentProcessId returns the id of my exe-file, not the excel.exe!
Ok.  If I were programming in VBA and wanted to find another instance of Excel, I could use:

for each p in Process.GetProcessesByName("EXCEL")

and p.id would be the PID number.  You could then ignore the current process and delete the others in the enumeration.

Here's a link that should help you with syntax on this approach:  http://msdn.microsoft.com/en-us/library/z3w4xdc9.aspx

Dave
Avatar of Cut_OS

ASKER

I've thinking about the possibility to delete the process by name "excel.exe" before. But this is in my opinion the last way to solve the problem, then it's possible to close a foreign excel process. Maybe there is another excel sheet open without my exe-file.

Should i try to write an own main-routine to close the form-function with return (int) or you have another idea?

Thank you for your cooperation!

Holger
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cut_OS

ASKER

OK. I'have written an own Main() function with a public (form)member as return value. So i needn't ExitProcess and excel.exe is closing clean. Thank you!