SubTestEgr
New member
- Joined
- Mar 3, 2010
- Messages
- 4
- Programming Experience
- 1-3
Hello, everyone. I have just joined your forum hoping to find an answer to a problem I have been pulling my hair out over.
I am trying for the first time to automate Excel 2007 using Visual Basic.NET. I have VS 2008 Professional and am running on a system with Windows XP 64-bit.
Here is the code as far as I've figured it out:
My problem is that when the code finishes Excel hangs in resident memory for about 20 seconds, then I get a window stating "Microsoft Office Excel has encountered a problem and needs to close." asking me if I want to send or not send a report.
I have tested this code starting with simply opening then closing the application on up through adding the Range Value "Test" and saving it. I have established that the problem occurs due to the presense of the line containing "SaveAs", but I cannot find a problem with the syntax.
Can someone please help? Thanks very much!!
I am trying for the first time to automate Excel 2007 using Visual Basic.NET. I have VS 2008 Professional and am running on a system with Windows XP 64-bit.
Here is the code as far as I've figured it out:
VB.NET:
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Imports System.IO
Public Class WindowsExcelTestForm
Private xlApp As Excel.Application = Nothing
Private xlWbs As Excel.Workbooks = Nothing
Private xlWb As Excel.Workbook = Nothing
Private xlWs As Excel.Worksheet = Nothing
Private xlRng As Excel.Range = Nothing
Private Sub MFRCOMO(ByVal o As Object)
Try
Marshal.FinalReleaseComObject(o)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly)
Finally
o = Nothing
End Try
End Sub
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Try
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
MFRCOMO(xlRng)
MFRCOMO(xlWs)
MFRCOMO(xlWb)
MFRCOMO(xlWbs)
MFRCOMO(xlApp)
Me.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly)
End Try
End Sub
Private Sub OpenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenButton.Click
If File.Exists("C:\TEST\Windows Excel Test 2.xlsx") Then File.Delete("C:\Test\Windows Excel Test 2.xlsx")
xlApp = CType(New Excel.Application, Excel.Application)
xlApp.Visible = True
xlWbs = CType(xlApp.Workbooks, Excel.Workbooks)
xlWb = CType(xlWbs.Add, Excel.Workbook)
xlWs = CType(xlWb.Sheets(Index:=1), Excel.Worksheet)
xlRng = CType(xlWs.Range("A1"), Excel.Range)
xlRng.Value = "Test"
xlWb.SaveAs(Filename:="C:\TEST\Windows Excel Test 2.xlsx") 'This is the line that causes the difference
xlWb.Close(SaveChanges:=False)
xlWbs.Close()
xlApp.Quit()
End Sub
End Class
My problem is that when the code finishes Excel hangs in resident memory for about 20 seconds, then I get a window stating "Microsoft Office Excel has encountered a problem and needs to close." asking me if I want to send or not send a report.
I have tested this code starting with simply opening then closing the application on up through adding the Range Value "Test" and saving it. I have established that the problem occurs due to the presense of the line containing "SaveAs", but I cannot find a problem with the syntax.
Can someone please help? Thanks very much!!