I'v been reading almost every article/post that there is about this problem, and I've made some progress in my app using Excel. But there's still one piece, at least, which I don't get. Some or both of these lines leaves Excel process in memory
Dim idx As Intege
For idx = 1 To
objRg = objWs.Columns(idx
objRg.AutoFit(
objRg.ColumnWidth = objRg.ColumnWidth +
'objRg.HorizontalAlignment = XlHAlign.xlHAlignLef
objRg.HorizontalAlignment = -413
Nex
where objRg, an Excel.Range, is both released and set to nothing (using NAR sub) and the same goes for objWs.
If I comment out the column width line and the HorizontalAlignment line (tried both versions above) everything works fine, but in this case some reference must be left after releasing all objects, but WHERE ??????????
Paul Clement - 24 Nov 2003 16:50 GMT
¤ I'v been reading almost every article/post that there is about this problem, and I've made some progress in my app using Excel. But there's still one piece, at least, which I don't get. Some or both of these lines leaves Excel process in memory:
¤
¤ Dim idx As Integer
¤ For idx = 1 To 9
¤ objRg = objWs.Columns(idx)
¤ objRg.AutoFit()
¤ objRg.ColumnWidth = objRg.ColumnWidth + 2
¤ 'objRg.HorizontalAlignment = XlHAlign.xlHAlignLeft
¤ objRg.HorizontalAlignment = -4131
¤ Next
¤
¤ where objRg, an Excel.Range, is both released and set to nothing (using NAR sub) and the same goes for objWs.
¤ If I comment out the column width line and the HorizontalAlignment line (tried both versions above) everything works fine, but in this case some reference must be left after releasing all objects, but WHERE ??????????
I've seen this issue so many times that I've just about given up trying to figure out why Excel
remains in memory. Instead, I either use GetObject to check if there is an existing version in
memory, or simply terminate the process with an API function call when I'm done with it:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal
lpWindowName As String) As Int32
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg
As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Public Function TerminateExcel()
Dim ClassName As String
Dim WindowHandle As Int32
Dim ReturnVal As Int32
Const WM_QUIT = &H12
Do
ClassName = "XLMain"
WindowHandle = FindWindow(ClassName, Nothing)
If WindowHandle Then
ReturnVal = PostMessage(WindowHandle, WM_QUIT, 0, 0)
End If
Loop Until WindowHandle = 0
End Function
Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)