Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2003

Tip: Looking for answers? Try searching our database.

Same old Excel problem (?)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rickard - 24 Nov 2003 10:31 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 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)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.