timv
March 16th, 2005, 05:20 AM
The problem is that from my visual basic program my excel application won't quit. It stays in the windows task manager even after xlApp.Quit() and xlApp=Nothing.
My program runs every day, so after 2 weeks I have 14 excel processes in the task manager and that's not what i want.
I read http://www.tushar-mehta.com/excel/vba/xl_doesnt_quit/ but didn't really get a solution.
my code (it opens excel files and updates their pivot tables):Module Module1
Sub Main()
' Get command line arguments
Dim CommandString() As String
CommandString = Split(Command, ",")
' Start Excel
Dim xlapp As Object 'Excel.Application
xlapp = CreateObject("Excel.Application")
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
' Refresh all workbooks
Dim counter As Long
Dim strFileName As String
For counter = 0 To UBound(CommandString)
Try
strFileName = CommandString(counter)
' Get the workbook
Dim xlbook As Object 'Excel.Workbook
' Some files are protected with a password, others aren't
xlbook = xlapp.Workbooks.Open(strFileName, password:="test")
' Add a module (to which the macro will be added) to the workbook
Dim xlmodule As Object 'VBComponent
xlmodule = xlbook.VBProject.VBComponents.Add(1) 'vbext_ct_StdModule
' Create the macro code
Dim strCode As String
strCode = _
"sub RefreshPivotTable()" & vbCr & _
" Sheets(1).PivotTables(1).RefreshTable" & vbCr & _
"end sub"
' Insert the macro code into the module
xlmodule.CodeModule.InsertLines(1, strCode)
' Let excel not display its alerts for saving
xlapp.DisplayAlerts = False
' Run the new macro!
xlapp.Run("RefreshPivotTable")
' Remove (from the workbook) and release module
xlbook.VBProject.VBComponents.Remove(xlmodule)
xlmodule = Nothing
xlbook.Close(SaveChanges:=True)
xlapp.DisplayAlerts = True
xlbook = Nothing
Catch Exc As System.Runtime.InteropServices.COMException
'Continue For 'caught a user entering a wrong password for this excel workbook
End Try
Next
' Quit Excel
xlapp.Quit()
xlapp = Nothing
End Sub
End Module
Does someone has a solution for what to change in my code that could solve this problem?
My program runs every day, so after 2 weeks I have 14 excel processes in the task manager and that's not what i want.
I read http://www.tushar-mehta.com/excel/vba/xl_doesnt_quit/ but didn't really get a solution.
my code (it opens excel files and updates their pivot tables):Module Module1
Sub Main()
' Get command line arguments
Dim CommandString() As String
CommandString = Split(Command, ",")
' Start Excel
Dim xlapp As Object 'Excel.Application
xlapp = CreateObject("Excel.Application")
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
' Refresh all workbooks
Dim counter As Long
Dim strFileName As String
For counter = 0 To UBound(CommandString)
Try
strFileName = CommandString(counter)
' Get the workbook
Dim xlbook As Object 'Excel.Workbook
' Some files are protected with a password, others aren't
xlbook = xlapp.Workbooks.Open(strFileName, password:="test")
' Add a module (to which the macro will be added) to the workbook
Dim xlmodule As Object 'VBComponent
xlmodule = xlbook.VBProject.VBComponents.Add(1) 'vbext_ct_StdModule
' Create the macro code
Dim strCode As String
strCode = _
"sub RefreshPivotTable()" & vbCr & _
" Sheets(1).PivotTables(1).RefreshTable" & vbCr & _
"end sub"
' Insert the macro code into the module
xlmodule.CodeModule.InsertLines(1, strCode)
' Let excel not display its alerts for saving
xlapp.DisplayAlerts = False
' Run the new macro!
xlapp.Run("RefreshPivotTable")
' Remove (from the workbook) and release module
xlbook.VBProject.VBComponents.Remove(xlmodule)
xlmodule = Nothing
xlbook.Close(SaveChanges:=True)
xlapp.DisplayAlerts = True
xlbook = Nothing
Catch Exc As System.Runtime.InteropServices.COMException
'Continue For 'caught a user entering a wrong password for this excel workbook
End Try
Next
' Quit Excel
xlapp.Quit()
xlapp = Nothing
End Sub
End Module
Does someone has a solution for what to change in my code that could solve this problem?