Question : How To Run an Excel Application from VB.Net

Hello

I have a small EXCEL application that I run from a VB.Net program  using SHELL.

When Excel loads, it *automatically* runs a Macro that OUTPUTS to a file.

The Macro then tells Excel to Quit itself.

It seems to work - because it OUTPUTS to the file correctly.

The problem is that I keep getting a Vista Message Box telling me that "Microsoft Excel Has Stopped Working" and it then starts to 'search' for the cause of the problem - eventually telling me that the Excel program has to close.

But the EXCEL program **did** work.

So, how can I stop this Vista Message Box from appearing?

Thank you.

Answer : How To Run an Excel Application from VB.Net


I wrote the following code for another problem, i have taken out the unnecessary stuff, I believe this is a much better way to approach opening the said workbook, then closing it, then quitting excel

notice the private function at the bottom, also this assumes you will add a reference to excel in the project and also use Imports Excel = microsoft.blah.blah.blha

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
Dim excelApp As Excel.Application
        Dim WB As Excel.Workbook
        Dim workbookName As String = "Test Workbook Name"

        'open Excel to minimized state
        excelApp = New Excel.Application()
        excelApp.Visible = CBool(Microsoft.Office.Core.MsoTriState.msoTrue)
        excelApp.WindowState = Excel.XlWindowState.xlMinimized

        'open workbook
        WB = excelApp.Workbooks.Open(workbookName, Microsoft.Office.Core.MsoTriState.msoFalse, _
                                     Microsoft.Office.Core.MsoTriState.msoFalse, _
                                     Microsoft.Office.Core.MsoTriState.msoTrue)
	
	WB.close
	excelApp.quit

        'need to release these com objects or they may remain open in the running services
        ReleaseObject(excelApp)
        ReleaseObject(WB)
        ReleaseObject(WS)
        ReleaseObject(target)

        excelApp = Nothing
        WB = Nothing
        WS = Nothing
        target = Nothing
        'Erase targetArray

        'run garbage collector
        GC.Collect()

    Private Sub ReleaseObject(ByVal o As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
        Catch ex As Exception
        Finally
            o = Nothing
        End Try

    End Sub
Random Solutions  
 
programming4us programming4us