Cannot kill Excel from taskmgr after Excel Export in VB 2014

Using VB 2013 on Windows 7 64-bit and Office 365 (Excel) 32 bit.

I have some code that exports a DataGridView to Excel.  The code works fine.
I had an issue with MS Access so I ran a "Quick Repair" on Office 365 and it fixed the Access issue.

Now my Export to Excel still works fine.  However, the code the kills the Excel.exe*32 that likes to hang out in taskmgr now generates an error.

CODE:

    Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
           (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    Const WM_QUIT As Object = &H12

 Dim xlApp As Excel.Application

... do the Excel export code successfully...

xlApp.Quit()

PostMessage(xlApp.Hwnd, WM_QUIT, 0, 0)              <<<<  CODE THAT NOW GENERATES THE ERROR   <<<<<

xlApp = Nothing

--------------------------------------------------------------------

Code that I have tried in place of the error generating line of code (this code does absolutely nothing):

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
GC.Collect()
GC.WaitForPendingFinalizers()

-----------------------------------------------------------------------------

Error looks like this:
Error killing EXCEL.EXE*32
Any ideas why this one line of code no longer works after I did a "Quick Repair" on Office 365 and how do I fix it?

What would the code look like to trap this error and "Resume Next"?

Thanks
sqdperuAsked:
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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
xlApp.Quit()

PostMessage(xlApp.Hwnd, WM_QUIT, 0, 0)              <<<<  CODE THAT NOW GENERATES THE ERROR   <<<<<

xlApp = Nothing

why do you need that line in your code at the first place?

xlApp.Quit()
xlApp = Nothing

Open in new window


should be enough to close and clear an Excel application instance.
0
sqdperuAuthor Commented:
In a perfect world MS products would play nicely together and you would be correct - logical that would be the case.  In the real world, you are incorrect.   This is a well known problem and the solution I was using worked until after the "Quick Repair" on Office.

The Excel.exe will stay out there in the background and will not go away until you close your VB program.  It use to stack them, one for every time you did the export.  Now it no longer appears to stack them, but it will also not go away without that PostMessage running successfully.

taskmgr of stuck Excel.exe
0
sqdperuAuthor Commented:
Additional things I have tried to no avail:

Ran the "Online Repair" to Office 365.

-------------------------------------------------------------------------------

 xlWorkSheet = Nothing
 xlWorkBook = Nothing
 xlApp.Visible = True
 xlApp.SendKeys("%{F4}", True)
 xlApp.Quit()

------------------------------------------------------------------------------

           xlApp.Quit()

            Try
                If Not xlWorkSheet Is Nothing Then
                    Marshal.ReleaseComObject(xlWorkSheet)
                End If
                If Not xlWorkBook Is Nothing Then
                    Marshal.ReleaseComObject(xlWorkBook)
                End If
                If Not xlApp Is Nothing Then
                    Marshal.ReleaseComObject(xlApp)
                End If
                GC.Collect()
                GC.WaitForPendingFinalizers()
                GC.Collect()
                GC.WaitForPendingFinalizers()
           Catch ex As Exception
                Exit Try
           End Try

        xlApp = Nothing

------------------------------------------------------------------------------

None of that will close the Excel.exe process.

And when I add the line "PostMessage(xlApp.Hwnd, WM_QUIT, 0, 0)" it causes the error in Excel.exe.
0
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

ArkCommented:
Note that each time you call Excel object VB.NET creates a wrapper to communicate with Excell. Correct sequence should be
Dim xlApp As New Excel.Application
Dim wBooks = xlApp.WorkBooks ' Vital. Dim wBook =  xlApp.WorkBooks.Add() is incorrect since workbooks wrapper stay in memory
Dim wBook = wBooks.Add()
'...............
wBook.Close()
wBooks.Close()
xlApp.Quit()
Marshal.ReleaseComObject(wBook)
Marshal.ReleaseComObject(wBooks)
Marshal.ReleaseComObject(xlApp)

see MSDN for details
2

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
sqdperuAuthor Commented:
This solution worked great.  Searching the internet and I never found this.  I was coding it wrong as Ark suggested.  The MSDN link was very helpful.  Also, for anyone using this solution note,  Excel.exe may not instantly leave TaskMgr after this code runs.  I have had it stay out there 40 seconds to a minute.  So be aware, you might think the code is not working, but you just have to wait sometimes.

Excellent solution.  Thanks!
0
ArkCommented:
Glad I could help. Note that MSDN suggests looping ReleaseComObject untill it returns 0. You can use smth like:
Private Sub ReleaseExcelObject(o As Object)
    Dim counter =0 'To prevent endless loop if smth goes wrong
    Do While Marshal.ReleaseComObject(o) >0
        counter +=1
        If counter > 30 then exit do
    Loop
    o = Nothing
End Sub

Open in new window

wBook.Close()
wBooks.Close()
xlApp.Quit()
ReleaseExcelObject(wBook)
ReleaseExcelObject(wBooks)
ReleaseExcelObject(xlApp) 

Open in new window

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
Office 365

From novice to tech pro — start learning today.