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!

Cut_OSAsked:
Who is Participating?
 
dlmilleConnect With a Mentor Commented:
I would do whatever was expedient at this point - good to have at least one working solution, then to improve with new learnings.  I like the ability to use return (int) to get results I'm looking for.
0
 
dlmilleCommented:
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
0
 
dlmilleCommented:
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

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dlmilleCommented:
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
0
 
Cut_OSAuthor Commented:
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


0
 
Cut_OSAuthor Commented:
GetCurrentProcessId returns the id of my exe-file, not the excel.exe!
0
 
dlmilleCommented:
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
0
 
Cut_OSAuthor Commented:
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
0
 
Cut_OSAuthor Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.