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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
dlmilleCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.