|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Can't destroy Excel Process; tried Just about everything!I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy Excel process, it still hangs in the taks manager' Processes as running. I am trying very simple code (see below), but Excel wont go away, I tried just about anything I could find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But NOTHING works, Any help will be appreciated. 'API declaration: Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long ' actual VB.NET Code: Public Const WM_DESTROY = &H2 Public Const WM_USER = &H400 Dim exApplication As New Excel.Application exApplication.DisplayAlerts = False exApplication.Caption = "Microsoft Excel (to close)" Dim wrk As Excel.Workbook wrk = exApplication.Workbooks.Add() Dim settings As New SiteSettings Dim dir As String = settings.getAppSettingValue("TempDirectory") Dim fileName As String = dir & Session.SessionID & ".xls" wrk.SaveAs(fileName) wrk.Close() NAR(wrk) wrk = Nothing exApplication.Quit() NAR(exApplication) exApplication = Nothing ' Kill process Dim hwnd As Long Dim lngResult As Long hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") If hwnd <> 0 Then lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) End If Private Sub NAR(ByRef o As Object) Dim i As Integer = -2 Try While (i <> 0 And i <> -1) i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) End While Catch Finally o = Nothing End Try End Sub ..NET languages do nopt clean up the same way as VBA does.
In addition to setting variables to Nothing, add the following at the end of your code to expedite garbage collection. GC.Collect() GC.WaitForPendingFinalizers() GC.Collect() GC.WaitForPendingFinalizers() And do not forget to Quit Excel. Show quoteHide quote "LP" <l*@a.com> wrote in message news:uOMFDDgPFHA.1176@TK2MSFTNGP12.phx.gbl... > Hello, > > I am trying to use .NET with Excel. I installed Office 2003 and selected > .NET programming suport option, so it installed all those PIA, as MS > sugests. But I can not find a way to destroy Excel process, it still hangs > in the taks manager' Processes as running. I am trying very simple code > (see below), but Excel wont go away, I tried just about anything I could > find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But > NOTHING works, Any help will be appreciated. > 'API declaration: > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal > lpClassName As String, ByVal lpWindowName As String) As Long > > Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal > hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As > Long) As Long > > ' actual VB.NET Code: > > Public Const WM_DESTROY = &H2 > > Public Const WM_USER = &H400 > > Dim exApplication As New Excel.Application > > exApplication.DisplayAlerts = False > > exApplication.Caption = "Microsoft Excel (to close)" > > Dim wrk As Excel.Workbook > > wrk = exApplication.Workbooks.Add() > > Dim settings As New SiteSettings > > Dim dir As String = settings.getAppSettingValue("TempDirectory") > > Dim fileName As String = dir & Session.SessionID & ".xls" > > wrk.SaveAs(fileName) > > wrk.Close() > > NAR(wrk) > > wrk = Nothing > > exApplication.Quit() > > NAR(exApplication) > > exApplication = Nothing > > ' Kill process > > Dim hwnd As Long > > Dim lngResult As Long > > hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") > > If hwnd <> 0 Then > > lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) > > lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) > > End If > > > > Private Sub NAR(ByRef o As Object) > > Dim i As Integer = -2 > > Try > > While (i <> 0 And i <> -1) > > i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > > End While > > Catch > > Finally > > o = Nothing > > End Try > > End Sub > > Thanks for the reply. I actually already had .Collect() below. I added:
GC.Collect() GC.WaitForPendingFinalizers() twice, but still Excel Process is running. Any other ideas? Show quoteHide quote "Howard Kaikow" <kai***@standards.com> wrote in message news:Oo94RHgPFHA.3384@TK2MSFTNGP10.phx.gbl... > .NET languages do nopt clean up the same way as VBA does. > > In addition to setting variables to Nothing, add the following at the end of > your code to expedite garbage collection. > GC.Collect() > GC.WaitForPendingFinalizers() > GC.Collect() > GC.WaitForPendingFinalizers() > > And do not forget to Quit Excel. > > -- > http://www.standards.com/; See Howard Kaikow's web site. > "LP" <l*@a.com> wrote in message > news:uOMFDDgPFHA.1176@TK2MSFTNGP12.phx.gbl... > > Hello, > > > > I am trying to use .NET with Excel. I installed Office 2003 and selected > > .NET programming suport option, so it installed all those PIA, as MS > > sugests. But I can not find a way to destroy Excel process, it still hangs > > in the taks manager' Processes as running. I am trying very simple code > > (see below), but Excel wont go away, I tried just about anything I could > > find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But > > NOTHING works, Any help will be appreciated. > > 'API declaration: > > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal > > lpClassName As String, ByVal lpWindowName As String) As Long > > > > Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" > (ByVal > > hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As > > Long) As Long > > > > ' actual VB.NET Code: > > > > Public Const WM_DESTROY = &H2 > > > > Public Const WM_USER = &H400 > > > > Dim exApplication As New Excel.Application > > > > exApplication.DisplayAlerts = False > > > > exApplication.Caption = "Microsoft Excel (to close)" > > > > Dim wrk As Excel.Workbook > > > > wrk = exApplication.Workbooks.Add() > > > > Dim settings As New SiteSettings > > > > Dim dir As String = settings.getAppSettingValue("TempDirectory") > > > > Dim fileName As String = dir & Session.SessionID & ".xls" > > > > wrk.SaveAs(fileName) > > > > wrk.Close() > > > > NAR(wrk) > > > > wrk = Nothing > > > > exApplication.Quit() > > > > NAR(exApplication) > > > > exApplication = Nothing > > > > ' Kill process > > > > Dim hwnd As Long > > > > Dim lngResult As Long > > > > hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") > > > > If hwnd <> 0 Then > > > > lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) > > > > lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) > > > > End If > > > > > > > > Private Sub NAR(ByRef o As Object) > > > > Dim i As Integer = -2 > > > > Try > > > > While (i <> 0 And i <> -1) > > > > i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > > > > End While > > > > Catch > > > > Finally > > > > o = Nothing > > > > End Try > > > > End Sub > > > > > > Don't forget to call Marshall.ReleaseComObject(yourXLinstanceObjectVariable)
for EACH COM object you've instanced after you quit Excel. Show quoteHide quote "LP" <l*@a.com> wrote in message news:uyaOEmgPFHA.1236@TK2MSFTNGP14.phx.gbl... > Thanks for the reply. I actually already had .Collect() below. I added: > GC.Collect() GC.WaitForPendingFinalizers() twice, but still Excel Process > is > running. Any other ideas? > > > > > > > > "Howard Kaikow" <kai***@standards.com> wrote in message > news:Oo94RHgPFHA.3384@TK2MSFTNGP10.phx.gbl... >> .NET languages do nopt clean up the same way as VBA does. >> >> In addition to setting variables to Nothing, add the following at the end > of >> your code to expedite garbage collection. >> GC.Collect() >> GC.WaitForPendingFinalizers() >> GC.Collect() >> GC.WaitForPendingFinalizers() >> >> And do not forget to Quit Excel. >> >> -- >> http://www.standards.com/; See Howard Kaikow's web site. >> "LP" <l*@a.com> wrote in message >> news:uOMFDDgPFHA.1176@TK2MSFTNGP12.phx.gbl... >> > Hello, >> > >> > I am trying to use .NET with Excel. I installed Office 2003 and >> > selected >> > .NET programming suport option, so it installed all those PIA, as MS >> > sugests. But I can not find a way to destroy Excel process, it still > hangs >> > in the taks manager' Processes as running. I am trying very simple code >> > (see below), but Excel wont go away, I tried just about anything I >> > could >> > find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But >> > NOTHING works, Any help will be appreciated. >> > 'API declaration: >> > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" > (ByVal >> > lpClassName As String, ByVal lpWindowName As String) As Long >> > >> > Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" >> (ByVal >> > hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As >> > Long) As Long >> > >> > ' actual VB.NET Code: >> > >> > Public Const WM_DESTROY = &H2 >> > >> > Public Const WM_USER = &H400 >> > >> > Dim exApplication As New Excel.Application >> > >> > exApplication.DisplayAlerts = False >> > >> > exApplication.Caption = "Microsoft Excel (to close)" >> > >> > Dim wrk As Excel.Workbook >> > >> > wrk = exApplication.Workbooks.Add() >> > >> > Dim settings As New SiteSettings >> > >> > Dim dir As String = settings.getAppSettingValue("TempDirectory") >> > >> > Dim fileName As String = dir & Session.SessionID & ".xls" >> > >> > wrk.SaveAs(fileName) >> > >> > wrk.Close() >> > >> > NAR(wrk) >> > >> > wrk = Nothing >> > >> > exApplication.Quit() >> > >> > NAR(exApplication) >> > >> > exApplication = Nothing >> > >> > ' Kill process >> > >> > Dim hwnd As Long >> > >> > Dim lngResult As Long >> > >> > hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") >> > >> > If hwnd <> 0 Then >> > >> > lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) >> > >> > lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) >> > >> > End If >> > >> > >> > >> > Private Sub NAR(ByRef o As Object) >> > >> > Dim i As Integer = -2 >> > >> > Try >> > >> > While (i <> 0 And i <> -1) >> > >> > i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) >> > >> > End While >> > >> > Catch >> > >> > Finally >> > >> > o = Nothing >> > >> > End Try >> > >> > End Sub >> > >> > >> >> > > Yes, I am doing that with NAR() function:
NAR(wrk) Private Sub NAR(ByRef o As Object) Dim i As Integer = -2 Try While (i <> 0 And i <> -1) i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) End While Catch Finally o = Nothing End Try End Sub "Scott M." <s-mar@nospam.nospam> wrote in message Marshall.ReleaseComObject(yourXLinstanceObjectVariable)news:ugiOiRjPFHA.2956@TK2MSFTNGP10.phx.gbl... > Don't forget to call Show quoteHide quote > for EACH COM object you've instanced after you quit Excel. > > > > "LP" <l*@a.com> wrote in message > news:uyaOEmgPFHA.1236@TK2MSFTNGP14.phx.gbl... > > Thanks for the reply. I actually already had .Collect() below. I added: > > GC.Collect() GC.WaitForPendingFinalizers() twice, but still Excel Process > > is > > running. Any other ideas? > > > > > > > > > > > > > > > > "Howard Kaikow" <kai***@standards.com> wrote in message > > news:Oo94RHgPFHA.3384@TK2MSFTNGP10.phx.gbl... > >> .NET languages do nopt clean up the same way as VBA does. > >> > >> In addition to setting variables to Nothing, add the following at the end > > of > >> your code to expedite garbage collection. > >> GC.Collect() > >> GC.WaitForPendingFinalizers() > >> GC.Collect() > >> GC.WaitForPendingFinalizers() > >> > >> And do not forget to Quit Excel. > >> > >> -- > >> http://www.standards.com/; See Howard Kaikow's web site. > >> "LP" <l*@a.com> wrote in message > >> news:uOMFDDgPFHA.1176@TK2MSFTNGP12.phx.gbl... > >> > Hello, > >> > > >> > I am trying to use .NET with Excel. I installed Office 2003 and > >> > selected > >> > .NET programming suport option, so it installed all those PIA, as MS > >> > sugests. But I can not find a way to destroy Excel process, it still > > hangs > >> > in the taks manager' Processes as running. I am trying very simple code > >> > (see below), but Excel wont go away, I tried just about anything I > >> > could > >> > find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But > >> > NOTHING works, Any help will be appreciated. > >> > 'API declaration: > >> > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" > > (ByVal > >> > lpClassName As String, ByVal lpWindowName As String) As Long > >> > > >> > Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" > >> (ByVal > >> > hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As > >> > Long) As Long > >> > > >> > ' actual VB.NET Code: > >> > > >> > Public Const WM_DESTROY = &H2 > >> > > >> > Public Const WM_USER = &H400 > >> > > >> > Dim exApplication As New Excel.Application > >> > > >> > exApplication.DisplayAlerts = False > >> > > >> > exApplication.Caption = "Microsoft Excel (to close)" > >> > > >> > Dim wrk As Excel.Workbook > >> > > >> > wrk = exApplication.Workbooks.Add() > >> > > >> > Dim settings As New SiteSettings > >> > > >> > Dim dir As String = settings.getAppSettingValue("TempDirectory") > >> > > >> > Dim fileName As String = dir & Session.SessionID & ".xls" > >> > > >> > wrk.SaveAs(fileName) > >> > > >> > wrk.Close() > >> > > >> > NAR(wrk) > >> > > >> > wrk = Nothing > >> > > >> > exApplication.Quit() > >> > > >> > NAR(exApplication) > >> > > >> > exApplication = Nothing > >> > > >> > ' Kill process > >> > > >> > Dim hwnd As Long > >> > > >> > Dim lngResult As Long > >> > > >> > hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") > >> > > >> > If hwnd <> 0 Then > >> > > >> > lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) > >> > > >> > lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) > >> > > >> > End If > >> > > >> > > >> > > >> > Private Sub NAR(ByRef o As Object) > >> > > >> > Dim i As Integer = -2 > >> > > >> > Try > >> > > >> > While (i <> 0 And i <> -1) > >> > > >> > i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > >> > > >> > End While > >> > > >> > Catch > >> > > >> > Finally > >> > > >> > o = Nothing > >> > > >> > End Try > >> > > >> > End Sub > >> > > >> > > >> > >> > > > > > > Something tells me that it's the way you've got the NAR method setup that is
the problem. Also, you should be setting your object variable to nothing before you call Marsahall.ReleaseComObject: "The ReleaseComObject method decrements the reference count of a runtime callable wrapper. When the reference count reached zero, the runtime releases all its references on the unmanaged COM object, and throws a System.NullReferenceException if you attempt to use the object further." Have you tried just using ReleaseCom object directly in your code rather than trying to create a "kill all" method? Show quoteHide quote "LP" <l*@a.com> wrote in message news:uTIg5YjPFHA.3156@TK2MSFTNGP15.phx.gbl... > Yes, I am doing that with NAR() function: > > NAR(wrk) > > > Private Sub NAR(ByRef o As Object) > Dim i As Integer = -2 > Try > While (i <> 0 And i <> -1) > i = > System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > > End While > > Catch > Finally > o = Nothing > End Try > End Sub > > > "Scott M." <s-mar@nospam.nospam> wrote in message > news:ugiOiRjPFHA.2956@TK2MSFTNGP10.phx.gbl... >> Don't forget to call > Marshall.ReleaseComObject(yourXLinstanceObjectVariable) >> for EACH COM object you've instanced after you quit Excel. >> >> >> >> "LP" <l*@a.com> wrote in message >> news:uyaOEmgPFHA.1236@TK2MSFTNGP14.phx.gbl... >> > Thanks for the reply. I actually already had .Collect() below. I added: >> > GC.Collect() GC.WaitForPendingFinalizers() twice, but still Excel > Process >> > is >> > running. Any other ideas? >> > >> > >> > >> > >> > >> > >> > >> > "Howard Kaikow" <kai***@standards.com> wrote in message >> > news:Oo94RHgPFHA.3384@TK2MSFTNGP10.phx.gbl... >> >> .NET languages do nopt clean up the same way as VBA does. >> >> >> >> In addition to setting variables to Nothing, add the following at the > end >> > of >> >> your code to expedite garbage collection. >> >> GC.Collect() >> >> GC.WaitForPendingFinalizers() >> >> GC.Collect() >> >> GC.WaitForPendingFinalizers() >> >> >> >> And do not forget to Quit Excel. >> >> >> >> -- >> >> http://www.standards.com/; See Howard Kaikow's web site. >> >> "LP" <l*@a.com> wrote in message >> >> news:uOMFDDgPFHA.1176@TK2MSFTNGP12.phx.gbl... >> >> > Hello, >> >> > >> >> > I am trying to use .NET with Excel. I installed Office 2003 and >> >> > selected >> >> > .NET programming suport option, so it installed all those PIA, as MS >> >> > sugests. But I can not find a way to destroy Excel process, it still >> > hangs >> >> > in the taks manager' Processes as running. I am trying very simple > code >> >> > (see below), but Excel wont go away, I tried just about anything I >> >> > could >> >> > find on MSDN or by Google. Even calling WIN32 API to destroy Excel. > But >> >> > NOTHING works, Any help will be appreciated. >> >> > 'API declaration: >> >> > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" >> > (ByVal >> >> > lpClassName As String, ByVal lpWindowName As String) As Long >> >> > >> >> > Public Declare Function SendMessage Lib "user32" Alias >> >> > "SendMessageA" >> >> (ByVal >> >> > hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam > As >> >> > Long) As Long >> >> > >> >> > ' actual VB.NET Code: >> >> > >> >> > Public Const WM_DESTROY = &H2 >> >> > >> >> > Public Const WM_USER = &H400 >> >> > >> >> > Dim exApplication As New Excel.Application >> >> > >> >> > exApplication.DisplayAlerts = False >> >> > >> >> > exApplication.Caption = "Microsoft Excel (to close)" >> >> > >> >> > Dim wrk As Excel.Workbook >> >> > >> >> > wrk = exApplication.Workbooks.Add() >> >> > >> >> > Dim settings As New SiteSettings >> >> > >> >> > Dim dir As String = settings.getAppSettingValue("TempDirectory") >> >> > >> >> > Dim fileName As String = dir & Session.SessionID & ".xls" >> >> > >> >> > wrk.SaveAs(fileName) >> >> > >> >> > wrk.Close() >> >> > >> >> > NAR(wrk) >> >> > >> >> > wrk = Nothing >> >> > >> >> > exApplication.Quit() >> >> > >> >> > NAR(exApplication) >> >> > >> >> > exApplication = Nothing >> >> > >> >> > ' Kill process >> >> > >> >> > Dim hwnd As Long >> >> > >> >> > Dim lngResult As Long >> >> > >> >> > hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") >> >> > >> >> > If hwnd <> 0 Then >> >> > >> >> > lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) >> >> > >> >> > lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) >> >> > >> >> > End If >> >> > >> >> > >> >> > >> >> > Private Sub NAR(ByRef o As Object) >> >> > >> >> > Dim i As Integer = -2 >> >> > >> >> > Try >> >> > >> >> > While (i <> 0 And i <> -1) >> >> > >> >> > i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) >> >> > >> >> > End While >> >> > >> >> > Catch >> >> > >> >> > Finally >> >> > >> >> > o = Nothing >> >> > >> >> > End Try >> >> > >> >> > End Sub >> >> > >> >> > >> >> >> >> >> > >> > >> >> > > LP,
I spent a few weeks dealing with the same thing. I finally discovered that how Excel objects are created and the order in which Excel objects are disposed is critical to getting the Excel Process to terminate. Try the following. the code opens a new instance of excel, adds a workbook, adds some sheets to the workbook, then adds some data to the first row of each sheet. Then it saves and exits Dim r, c, s as Integer Dim xl As Excel.Application Dim wbs As Excel.Workbooks Dim wb As Excel.Workbook Dim ws As Excel.Worksheet Dim rng As Excel.Range Dim st As Excel.Sheets Dim SName() As String = {"First", "Second", "Third", "Fourth"} Dim settings As New SiteSettings Dim dir As String = settings.getAppSettingValue("TempDirectory") Dim fileName As String = dir & Session.SessionID & ".xls" r = 1 xl = New Excel.Application xl.Visible = False xl.DisplayAlerts = False xl.SheetsInNewWorkbook = 1 wbs = xl.Workbooks wb = wbs.Add st = wb.Sheets ws = wb.ActiveSheet ws.Name = "Summary" For s = 0 To Sname.Length st.Add() ws = wb.ActiveSheet ws.Name = Sname(s) For c = 1 To 10 ws.Cells.Item(r, c) = ¡°X¡± Next Next xl.ActiveWorkbook.SaveAs(filename) xl.ActiveWorkbook.Saved = True NAR(rng) NAR(st) NAR(ws) xl.ActiveWorkbook.Close() NAR(wb) NAR(wbs) xl.Quit() NAR(xl) GC.Collect() Private Sub NAR(ByVal o As Object) Dim i, j As Integer Try For i = 1 To System.Runtime.InteropServices.Marshal.ReleaseComObject(o) j = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) Next Catch Finally o = Nothing End Try End Sub Let me know how it goes! HTH Lee Show quoteHide quote "LP" <l*@a.com> wrote in message news:uyaOEmgPFHA.1236@TK2MSFTNGP14.phx.gbl... > Thanks for the reply. I actually already had .Collect() below. I added: > GC.Collect() GC.WaitForPendingFinalizers() twice, but still Excel Process > is > running. Any other ideas? > > > > > > > > "Howard Kaikow" <kai***@standards.com> wrote in message > news:Oo94RHgPFHA.3384@TK2MSFTNGP10.phx.gbl... >> .NET languages do nopt clean up the same way as VBA does. >> >> In addition to setting variables to Nothing, add the following at the end > of >> your code to expedite garbage collection. >> GC.Collect() >> GC.WaitForPendingFinalizers() >> GC.Collect() >> GC.WaitForPendingFinalizers() >> >> And do not forget to Quit Excel. >> >> -- >> http://www.standards.com/; See Howard Kaikow's web site. >> "LP" <l*@a.com> wrote in message >> news:uOMFDDgPFHA.1176@TK2MSFTNGP12.phx.gbl... >> > Hello, >> > >> > I am trying to use .NET with Excel. I installed Office 2003 and >> > selected >> > .NET programming suport option, so it installed all those PIA, as MS >> > sugests. But I can not find a way to destroy Excel process, it still > hangs >> > in the taks manager' Processes as running. I am trying very simple code >> > (see below), but Excel wont go away, I tried just about anything I >> > could >> > find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But >> > NOTHING works, Any help will be appreciated. >> > 'API declaration: >> > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" > (ByVal >> > lpClassName As String, ByVal lpWindowName As String) As Long >> > >> > Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" >> (ByVal >> > hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As >> > Long) As Long >> > >> > ' actual VB.NET Code: >> > >> > Public Const WM_DESTROY = &H2 >> > >> > Public Const WM_USER = &H400 >> > >> > Dim exApplication As New Excel.Application >> > >> > exApplication.DisplayAlerts = False >> > >> > exApplication.Caption = "Microsoft Excel (to close)" >> > >> > Dim wrk As Excel.Workbook >> > >> > wrk = exApplication.Workbooks.Add() >> > >> > Dim settings As New SiteSettings >> > >> > Dim dir As String = settings.getAppSettingValue("TempDirectory") >> > >> > Dim fileName As String = dir & Session.SessionID & ".xls" >> > >> > wrk.SaveAs(fileName) >> > >> > wrk.Close() >> > >> > NAR(wrk) >> > >> > wrk = Nothing >> > >> > exApplication.Quit() >> > >> > NAR(exApplication) >> > >> > exApplication = Nothing >> > >> > ' Kill process >> > >> > Dim hwnd As Long >> > >> > Dim lngResult As Long >> > >> > hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") >> > >> > If hwnd <> 0 Then >> > >> > lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) >> > >> > lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) >> > >> > End If >> > >> > >> > >> > Private Sub NAR(ByRef o As Object) >> > >> > Dim i As Integer = -2 >> > >> > Try >> > >> > While (i <> 0 And i <> -1) >> > >> > i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) >> > >> > End While >> > >> > Catch >> > >> > Finally >> > >> > o = Nothing >> > >> > End Try >> > >> > End Sub >> > >> > >> >> > > Thanks lgbjr.
I tried your code, but unfortunatly it's still not working. I even tried enchancing your code; destroying selected range and , looping through worksheets and workbooks in xl application and destroying them and still doesnt work. It seems to me it's somekind of bug in Excel.Interop, I really want to hear from MS people about this. MSDN library doesn't seem to have any usefull info about this issue. Show quoteHide quote "lgbjr" <lgbjr@online.nospam> wrote in message news:eQ933ijPFHA.2144@TK2MSFTNGP09.phx.gbl... > LP, > > I spent a few weeks dealing with the same thing. I finally discovered that > how Excel objects are created and the order in which Excel objects are > disposed is critical to getting the Excel Process to terminate. > > Try the following. the code opens a new instance of excel, adds a workbook, > adds some sheets to the workbook, then adds some data to the first row of > each sheet. Then it saves and exits > > Dim r, c, s as Integer > > Dim xl As Excel.Application > > Dim wbs As Excel.Workbooks > > Dim wb As Excel.Workbook > > Dim ws As Excel.Worksheet > > Dim rng As Excel.Range > > Dim st As Excel.Sheets > > Dim SName() As String = {"First", "Second", "Third", "Fourth"} > > Dim settings As New SiteSettings > Dim dir As String = settings.getAppSettingValue("TempDirectory") > Dim fileName As String = dir & Session.SessionID & ".xls" > > > > r = 1 > > xl = New Excel.Application > > xl.Visible = False > > xl.DisplayAlerts = False > > xl.SheetsInNewWorkbook = 1 > > wbs = xl.Workbooks > > wb = wbs.Add > > st = wb.Sheets > > ws = wb.ActiveSheet > > ws.Name = "Summary" > > For s = 0 To Sname.Length > > st.Add() > > ws = wb.ActiveSheet > > ws.Name = Sname(s) > > For c = 1 To 10 > > ws.Cells.Item(r, c) = ¡°X¡± > > Next > > Next > > > > xl.ActiveWorkbook.SaveAs(filename) > > xl.ActiveWorkbook.Saved = True > > NAR(rng) > > NAR(st) > > NAR(ws) > > xl.ActiveWorkbook.Close() > > NAR(wb) > > NAR(wbs) > > xl.Quit() > > NAR(xl) > > GC.Collect() > > > > Private Sub NAR(ByVal o As Object) > > Dim i, j As Integer > > Try > > For i = 1 To > System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > > j = > System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > > Next > > Catch > > Finally > > o = Nothing > > End Try > > End Sub > > > Let me know how it goes! > > HTH > Lee > "LP" <l*@a.com> wrote in message > news:uyaOEmgPFHA.1236@TK2MSFTNGP14.phx.gbl... > > Thanks for the reply. I actually already had .Collect() below. I added: > > GC.Collect() GC.WaitForPendingFinalizers() twice, but still Excel Process > > is > > running. Any other ideas? > > > > > > > > > > > > > > > > "Howard Kaikow" <kai***@standards.com> wrote in message > > news:Oo94RHgPFHA.3384@TK2MSFTNGP10.phx.gbl... > >> .NET languages do nopt clean up the same way as VBA does. > >> > >> In addition to setting variables to Nothing, add the following at the end > > of > >> your code to expedite garbage collection. > >> GC.Collect() > >> GC.WaitForPendingFinalizers() > >> GC.Collect() > >> GC.WaitForPendingFinalizers() > >> > >> And do not forget to Quit Excel. > >> > >> -- > >> http://www.standards.com/; See Howard Kaikow's web site. > >> "LP" <l*@a.com> wrote in message > >> news:uOMFDDgPFHA.1176@TK2MSFTNGP12.phx.gbl... > >> > Hello, > >> > > >> > I am trying to use .NET with Excel. I installed Office 2003 and > >> > selected > >> > .NET programming suport option, so it installed all those PIA, as MS > >> > sugests. But I can not find a way to destroy Excel process, it still > > hangs > >> > in the taks manager' Processes as running. I am trying very simple code > >> > (see below), but Excel wont go away, I tried just about anything I > >> > could > >> > find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But > >> > NOTHING works, Any help will be appreciated. > >> > 'API declaration: > >> > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" > > (ByVal > >> > lpClassName As String, ByVal lpWindowName As String) As Long > >> > > >> > Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" > >> (ByVal > >> > hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As > >> > Long) As Long > >> > > >> > ' actual VB.NET Code: > >> > > >> > Public Const WM_DESTROY = &H2 > >> > > >> > Public Const WM_USER = &H400 > >> > > >> > Dim exApplication As New Excel.Application > >> > > >> > exApplication.DisplayAlerts = False > >> > > >> > exApplication.Caption = "Microsoft Excel (to close)" > >> > > >> > Dim wrk As Excel.Workbook > >> > > >> > wrk = exApplication.Workbooks.Add() > >> > > >> > Dim settings As New SiteSettings > >> > > >> > Dim dir As String = settings.getAppSettingValue("TempDirectory") > >> > > >> > Dim fileName As String = dir & Session.SessionID & ".xls" > >> > > >> > wrk.SaveAs(fileName) > >> > > >> > wrk.Close() > >> > > >> > NAR(wrk) > >> > > >> > wrk = Nothing > >> > > >> > exApplication.Quit() > >> > > >> > NAR(exApplication) > >> > > >> > exApplication = Nothing > >> > > >> > ' Kill process > >> > > >> > Dim hwnd As Long > >> > > >> > Dim lngResult As Long > >> > > >> > hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") > >> > > >> > If hwnd <> 0 Then > >> > > >> > lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) > >> > > >> > lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) > >> > > >> > End If > >> > > >> > > >> > > >> > Private Sub NAR(ByRef o As Object) > >> > > >> > Dim i As Integer = -2 > >> > > >> > Try > >> > > >> > While (i <> 0 And i <> -1) > >> > > >> > i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > >> > > >> > End While > >> > > >> > Catch > >> > > >> > Finally > >> > > >> > o = Nothing > >> > > >> > End Try > >> > > >> > End Sub > >> > > >> > > >> > >> > > > > > > Hmm,
For me, it does work. And believe me, I tried many different iteration over the course of a few weeks trying to get rid of the Excel process. Hopefully MS will have some additional info, other than the MSDN article with the NAR function. Actually, I haven't worked on that part of my code in some time. I'll have to run it and verify that it does actually work. I have it marked as completed on my task list. But, maybe, that was done too soon (and I just got tired of working on it :-)) I'll let you know. cheers Lee Show quoteHide quote "LP" <l*@a.com> wrote in message news:eEkArokPFHA.1268@TK2MSFTNGP14.phx.gbl... > Thanks lgbjr. > > I tried your code, but unfortunatly it's still not working. I even tried > enchancing your code; destroying selected range and , looping through > worksheets and workbooks in xl application and destroying them and still > doesnt work. It seems to me it's somekind of bug in Excel.Interop, I > really > want to hear from MS people about this. MSDN library doesn't seem to have > any usefull info about this issue. > > > "lgbjr" <lgbjr@online.nospam> wrote in message > news:eQ933ijPFHA.2144@TK2MSFTNGP09.phx.gbl... >> LP, >> >> I spent a few weeks dealing with the same thing. I finally discovered >> that >> how Excel objects are created and the order in which Excel objects are >> disposed is critical to getting the Excel Process to terminate. >> >> Try the following. the code opens a new instance of excel, adds a > workbook, >> adds some sheets to the workbook, then adds some data to the first row of >> each sheet. Then it saves and exits >> >> Dim r, c, s as Integer >> >> Dim xl As Excel.Application >> >> Dim wbs As Excel.Workbooks >> >> Dim wb As Excel.Workbook >> >> Dim ws As Excel.Worksheet >> >> Dim rng As Excel.Range >> >> Dim st As Excel.Sheets >> >> Dim SName() As String = {"First", "Second", "Third", "Fourth"} >> >> Dim settings As New SiteSettings >> Dim dir As String = settings.getAppSettingValue("TempDirectory") >> Dim fileName As String = dir & Session.SessionID & ".xls" >> >> >> >> r = 1 >> >> xl = New Excel.Application >> >> xl.Visible = False >> >> xl.DisplayAlerts = False >> >> xl.SheetsInNewWorkbook = 1 >> >> wbs = xl.Workbooks >> >> wb = wbs.Add >> >> st = wb.Sheets >> >> ws = wb.ActiveSheet >> >> ws.Name = "Summary" >> >> For s = 0 To Sname.Length >> >> st.Add() >> >> ws = wb.ActiveSheet >> >> ws.Name = Sname(s) >> >> For c = 1 To 10 >> >> ws.Cells.Item(r, c) = ¡°X¡± >> >> Next >> >> Next >> >> >> >> xl.ActiveWorkbook.SaveAs(filename) >> >> xl.ActiveWorkbook.Saved = True >> >> NAR(rng) >> >> NAR(st) >> >> NAR(ws) >> >> xl.ActiveWorkbook.Close() >> >> NAR(wb) >> >> NAR(wbs) >> >> xl.Quit() >> >> NAR(xl) >> >> GC.Collect() >> >> >> >> Private Sub NAR(ByVal o As Object) >> >> Dim i, j As Integer >> >> Try >> >> For i = 1 To >> System.Runtime.InteropServices.Marshal.ReleaseComObject(o) >> >> j = >> System.Runtime.InteropServices.Marshal.ReleaseComObject(o) >> >> Next >> >> Catch >> >> Finally >> >> o = Nothing >> >> End Try >> >> End Sub >> >> >> Let me know how it goes! >> >> HTH >> Lee >> "LP" <l*@a.com> wrote in message >> news:uyaOEmgPFHA.1236@TK2MSFTNGP14.phx.gbl... >> > Thanks for the reply. I actually already had .Collect() below. I added: >> > GC.Collect() GC.WaitForPendingFinalizers() twice, but still Excel > Process >> > is >> > running. Any other ideas? >> > >> > >> > >> > >> > >> > >> > >> > "Howard Kaikow" <kai***@standards.com> wrote in message >> > news:Oo94RHgPFHA.3384@TK2MSFTNGP10.phx.gbl... >> >> .NET languages do nopt clean up the same way as VBA does. >> >> >> >> In addition to setting variables to Nothing, add the following at the > end >> > of >> >> your code to expedite garbage collection. >> >> GC.Collect() >> >> GC.WaitForPendingFinalizers() >> >> GC.Collect() >> >> GC.WaitForPendingFinalizers() >> >> >> >> And do not forget to Quit Excel. >> >> >> >> -- >> >> http://www.standards.com/; See Howard Kaikow's web site. >> >> "LP" <l*@a.com> wrote in message >> >> news:uOMFDDgPFHA.1176@TK2MSFTNGP12.phx.gbl... >> >> > Hello, >> >> > >> >> > I am trying to use .NET with Excel. I installed Office 2003 and >> >> > selected >> >> > .NET programming suport option, so it installed all those PIA, as MS >> >> > sugests. But I can not find a way to destroy Excel process, it still >> > hangs >> >> > in the taks manager' Processes as running. I am trying very simple > code >> >> > (see below), but Excel wont go away, I tried just about anything I >> >> > could >> >> > find on MSDN or by Google. Even calling WIN32 API to destroy Excel. > But >> >> > NOTHING works, Any help will be appreciated. >> >> > 'API declaration: >> >> > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" >> > (ByVal >> >> > lpClassName As String, ByVal lpWindowName As String) As Long >> >> > >> >> > Public Declare Function SendMessage Lib "user32" Alias >> >> > "SendMessageA" >> >> (ByVal >> >> > hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam > As >> >> > Long) As Long >> >> > >> >> > ' actual VB.NET Code: >> >> > >> >> > Public Const WM_DESTROY = &H2 >> >> > >> >> > Public Const WM_USER = &H400 >> >> > >> >> > Dim exApplication As New Excel.Application >> >> > >> >> > exApplication.DisplayAlerts = False >> >> > >> >> > exApplication.Caption = "Microsoft Excel (to close)" >> >> > >> >> > Dim wrk As Excel.Workbook >> >> > >> >> > wrk = exApplication.Workbooks.Add() >> >> > >> >> > Dim settings As New SiteSettings >> >> > >> >> > Dim dir As String = settings.getAppSettingValue("TempDirectory") >> >> > >> >> > Dim fileName As String = dir & Session.SessionID & ".xls" >> >> > >> >> > wrk.SaveAs(fileName) >> >> > >> >> > wrk.Close() >> >> > >> >> > NAR(wrk) >> >> > >> >> > wrk = Nothing >> >> > >> >> > exApplication.Quit() >> >> > >> >> > NAR(exApplication) >> >> > >> >> > exApplication = Nothing >> >> > >> >> > ' Kill process >> >> > >> >> > Dim hwnd As Long >> >> > >> >> > Dim lngResult As Long >> >> > >> >> > hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") >> >> > >> >> > If hwnd <> 0 Then >> >> > >> >> > lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) >> >> > >> >> > lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) >> >> > >> >> > End If >> >> > >> >> > >> >> > >> >> > Private Sub NAR(ByRef o As Object) >> >> > >> >> > Dim i As Integer = -2 >> >> > >> >> > Try >> >> > >> >> > While (i <> 0 And i <> -1) >> >> > >> >> > i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) >> >> > >> >> > End While >> >> > >> >> > Catch >> >> > >> >> > Finally >> >> > >> >> > o = Nothing >> >> > >> >> > End Try >> >> > >> >> > End Sub >> >> > >> >> > >> >> >> >> >> > >> > >> >> > > Is the Google Desktop installed? For some weird reason, its presence
prevents Excel to exit. /Fredrik I've never had much luck getting OFFICE apps to run down cleanly and
consistently. My approach is to 1) scan processes prior to CreateObject. (accumulate PID integer in an ArrayList) 2) CreateObject 3) scan processes again, see what is new (verify PID integer using ArrayList.Contains) The time frame between 1 and 3 must be as short as possible. Sanity check the process name if the above makes you uncomfortable. Then once you have tried every trick you can think of to tell EXCEL to shut down in a nice way, then you can see if/when the process has exited with the .NET "Process" object. Give it a moment or two. If it still has not gone away, then kill it! To me, there seems to be too much voodoo in getting office automation to avoid leaving orphaned processes out there. HTH - Lee Thanks Lee,
> My approach is to What do you mean scan process, could elaborate on this? Any code snippets> 1) scan processes prior to CreateObject. > (accumulate PID integer in an ArrayList) > 2) CreateObject > 3) scan processes again, see what is new > (verify PID integer using ArrayList.Contains) would be nice. I did get it to work to certain point; just to open a file, add a workbook and "save as" it. From everyone's comments on this thread and from my own painful experience, I did figure out the pattern behind cleaning up excel. Basically ReleaseComObject everything that excel object "touches" even if it's not obvious. For example my code never directly referenced worksheets collection, but I did referenced workbooks, but I still had to get reference to worksheets and ReleaseComObject it to kill excel process. I guess underneath the covers workbooks object creates worksheets reference. So I got that part to work, but when I started actually writing data to excel, I quickly realized it's not going to be worth my time. Because I literally had to Release every cell I referenced. And even then I could miss something not so obvious. So I decided to do all excel related work in classic asp. I wasn't too happy about going back to old scripting world, but it worked out beautifully for me. It took me a couple hours to finish export to excel functionality when I strugeled in .NET for 4 days and still couldn't get it to work flawlesly. I really doubt it makes any sense to use Excel in .NET, because you're spending more time writing code to quit excel gracefully than actually writing business rules code which really matters. I will be making a few more posts about other Excel and ASP.NET options . I would appreciate any further comments. Thank you Show quoteHide quote "Lee Gillie" <Lee@nospam.odp.com> wrote in message news:uiN0mg5PFHA.4052@TK2MSFTNGP12.phx.gbl... > I've never had much luck getting OFFICE apps to run down cleanly and > consistently. > > My approach is to > 1) scan processes prior to CreateObject. > (accumulate PID integer in an ArrayList) > 2) CreateObject > 3) scan processes again, see what is new > (verify PID integer using ArrayList.Contains) > > The time frame between 1 and 3 must be as short as possible. > Sanity check the process name if the above makes you uncomfortable. > > Then once you have tried every trick you can think of to tell EXCEL to > shut down in a nice way, then you can see if/when the process has exited > with the .NET "Process" object. Give it a moment or two. If it still has > not gone away, then kill it! > > To me, there seems to be too much voodoo in getting office automation to > avoid leaving orphaned processes out there. > > HTH - Lee You know, you could write a VB 6.0 component that does all this Excel stuff
and then call that from .NET. You would only have to worry about ReleaseComObject on the component, not all the Excel objects. There really isn't any great benefit of using Excel directly in .NET as you are still using COM InterOp. Making an component that talks to Excel for you still uses COM InterOp, but make the .NET to COM part a bit cleaner. Show quoteHide quote "LP" <l*@a.com> wrote in message news:uB0mgj8PFHA.1884@TK2MSFTNGP15.phx.gbl... > Thanks Lee, > >> My approach is to >> 1) scan processes prior to CreateObject. >> (accumulate PID integer in an ArrayList) >> 2) CreateObject >> 3) scan processes again, see what is new >> (verify PID integer using ArrayList.Contains) > > What do you mean scan process, could elaborate on this? Any code snippets > would be nice. > > I did get it to work to certain point; just to open a file, add a workbook > and "save as" it. From everyone's comments on this thread and from my own > painful experience, I did figure out the pattern behind cleaning up excel. > Basically ReleaseComObject everything that excel object "touches" even if > it's not obvious. For example my code never directly referenced worksheets > collection, but I did referenced workbooks, but I still had to get > reference > to worksheets and ReleaseComObject it to kill excel process. I guess > underneath the covers workbooks object creates worksheets reference. > > So I got that part to work, but when I started actually writing data to > excel, I quickly realized it's not going to be worth my time. Because I > literally had to Release every cell I referenced. And even then I could > miss > something not so obvious. > > So I decided to do all excel related work in classic asp. I wasn't too > happy > about going back to old scripting world, but it worked out beautifully for > me. It took me a couple hours to finish export to excel functionality when > I > strugeled in .NET for 4 days and still couldn't get it to work flawlesly. > I really doubt it makes any sense to use Excel in .NET, because you're > spending more time writing code to quit excel gracefully than actually > writing business rules code which really matters. I will be making a few > more posts about other Excel and ASP.NET options . I would appreciate any > further comments. > > Thank you > > "Lee Gillie" <Lee@nospam.odp.com> wrote in message > news:uiN0mg5PFHA.4052@TK2MSFTNGP12.phx.gbl... >> I've never had much luck getting OFFICE apps to run down cleanly and >> consistently. >> >> My approach is to >> 1) scan processes prior to CreateObject. >> (accumulate PID integer in an ArrayList) >> 2) CreateObject >> 3) scan processes again, see what is new >> (verify PID integer using ArrayList.Contains) >> >> The time frame between 1 and 3 must be as short as possible. >> Sanity check the process name if the above makes you uncomfortable. >> >> Then once you have tried every trick you can think of to tell EXCEL to >> shut down in a nice way, then you can see if/when the process has exited >> with the .NET "Process" object. Give it a moment or two. If it still has >> not gone away, then kill it! >> >> To me, there seems to be too much voodoo in getting office automation to >> avoid leaving orphaned processes out there. >> >> HTH - Lee > > Yes, I tired that, but I got a permision error, I didn't have time to deal
with that. That's a topic for another post anyway. Show quoteHide quote "Scott M." <s-mar@nospam.nospam> wrote in message news:eGvOfODQFHA.4028@tk2msftngp13.phx.gbl... > You know, you could write a VB 6.0 component that does all this Excel stuff > and then call that from .NET. You would only have to worry about > ReleaseComObject on the component, not all the Excel objects. > > There really isn't any great benefit of using Excel directly in .NET as you > are still using COM InterOp. Making an component that talks to Excel for > you still uses COM InterOp, but make the .NET to COM part a bit cleaner. > > > "LP" <l*@a.com> wrote in message > news:uB0mgj8PFHA.1884@TK2MSFTNGP15.phx.gbl... > > Thanks Lee, > > > >> My approach is to > >> 1) scan processes prior to CreateObject. > >> (accumulate PID integer in an ArrayList) > >> 2) CreateObject > >> 3) scan processes again, see what is new > >> (verify PID integer using ArrayList.Contains) > > > > What do you mean scan process, could elaborate on this? Any code snippets > > would be nice. > > > > I did get it to work to certain point; just to open a file, add a workbook > > and "save as" it. From everyone's comments on this thread and from my own > > painful experience, I did figure out the pattern behind cleaning up excel. > > Basically ReleaseComObject everything that excel object "touches" even if > > it's not obvious. For example my code never directly referenced worksheets > > collection, but I did referenced workbooks, but I still had to get > > reference > > to worksheets and ReleaseComObject it to kill excel process. I guess > > underneath the covers workbooks object creates worksheets reference. > > > > So I got that part to work, but when I started actually writing data to > > excel, I quickly realized it's not going to be worth my time. Because I > > literally had to Release every cell I referenced. And even then I could > > miss > > something not so obvious. > > > > So I decided to do all excel related work in classic asp. I wasn't too > > happy > > about going back to old scripting world, but it worked out beautifully for > > me. It took me a couple hours to finish export to excel functionality when > > I > > strugeled in .NET for 4 days and still couldn't get it to work flawlesly. > > I really doubt it makes any sense to use Excel in .NET, because you're > > spending more time writing code to quit excel gracefully than actually > > writing business rules code which really matters. I will be making a few > > more posts about other Excel and ASP.NET options . I would appreciate any > > further comments. > > > > Thank you > > > > "Lee Gillie" <Lee@nospam.odp.com> wrote in message > > news:uiN0mg5PFHA.4052@TK2MSFTNGP12.phx.gbl... > >> I've never had much luck getting OFFICE apps to run down cleanly and > >> consistently. > >> > >> My approach is to > >> 1) scan processes prior to CreateObject. > >> (accumulate PID integer in an ArrayList) > >> 2) CreateObject > >> 3) scan processes again, see what is new > >> (verify PID integer using ArrayList.Contains) > >> > >> The time frame between 1 and 3 must be as short as possible. > >> Sanity check the process name if the above makes you uncomfortable. > >> > >> Then once you have tried every trick you can think of to tell EXCEL to > >> shut down in a nice way, then you can see if/when the process has exited > >> with the .NET "Process" object. Give it a moment or two. If it still has > >> not gone away, then kill it! > >> > >> To me, there seems to be too much voodoo in getting office automation to > >> avoid leaving orphaned processes out there. > >> > >> HTH - Lee > > > > > > I noticed that all of the responces are in VB!
I just tried this in C# I seemed to have much better Luck! TEH Show quoteHide quote "LP" wrote: > Hello, > > I am trying to use .NET with Excel. I installed Office 2003 and selected > ..NET programming suport option, so it installed all those PIA, as MS > sugests. But I can not find a way to destroy Excel process, it still hangs > in the taks manager' Processes as running. I am trying very simple code > (see below), but Excel wont go away, I tried just about anything I could > find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But > NOTHING works, Any help will be appreciated. > 'API declaration: > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal > lpClassName As String, ByVal lpWindowName As String) As Long > > Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal > hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As > Long) As Long > > ' actual VB.NET Code: > > Public Const WM_DESTROY = &H2 > > Public Const WM_USER = &H400 > > Dim exApplication As New Excel.Application > > exApplication.DisplayAlerts = False > > exApplication.Caption = "Microsoft Excel (to close)" > > Dim wrk As Excel.Workbook > > wrk = exApplication.Workbooks.Add() > > Dim settings As New SiteSettings > > Dim dir As String = settings.getAppSettingValue("TempDirectory") > > Dim fileName As String = dir & Session.SessionID & ".xls" > > wrk.SaveAs(fileName) > > wrk.Close() > > NAR(wrk) > > wrk = Nothing > > exApplication.Quit() > > NAR(exApplication) > > exApplication = Nothing > > ' Kill process > > Dim hwnd As Long > > Dim lngResult As Long > > hwnd = FindWindow("XLMAIN", "Microsoft Excel (to close)") > > If hwnd <> 0 Then > > lngResult = SendMessage(hwnd, WM_USER + 18, 0, 0) > > lngResult = SendMessage(hwnd, WM_DESTROY, 0, 0) > > End If > > > > Private Sub NAR(ByRef o As Object) > > Dim i As Integer = -2 > > Try > > While (i <> 0 And i <> -1) > > i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > > End While > > Catch > > Finally > > o = Nothing > > End Try > > End Sub > > >
NullReferenceException when setting to array list
Visual Basic.net Access vs SQL How to catch a right-click TAB (key) to next control instead of next column (in datagrid) Writing to Web Server Naming convention for objects Re: Freeze Column in DataGrid why does my app.productionversion not auto increment? Visual Basic.net and SQL Server |
|||||||||||||||||||||||