L
LP
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
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