Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27233

VB 2010 Express: Yet Another Stubborn Excel Process Won't End

$
0
0
I have been over this before in the forum and first off apologize for being so dense on this subject. I really thought I understood it after the first go round.

The project I'm working on was recently revised extensively by the sales team. So in a nutshell, and without being able to post too much of the code due to Non-Disclosure Agreements I'm bound to here is my problem.

I open Excel 2003 with the standard method:

Code:

  Dim vExcelApp As Excel.Application = Nothing
  Dim vExcelWorkbooks As Excel.Workbooks = Nothing
  Dim vExcelWorkbook As Excel.Workbook = Nothing
  Dim vExcelSheet1 As Excel.Worksheet = Nothing
  Dim vExcelSheet2 As Excel.Worksheet = Nothing
  Dim vExcelRange1 As Excel.Range = Nothing
  Dim vNovellPath As String = "\\a_very_long_novell_path\"
  Dim vExcelFile As String = NovellPath & "MyExcelFile.xls"

  vExcelApp = New Excel.Application
  vExcelApp.DisplayAlerts = False
  vExcelApp.Visible = False

  vExcelWorkbooks = vExcelApp.Workbooks
  vExcelWorkbook = vExcelApp.Workbooks.Open(vExcelFile)
  vExcelSheet1 = CType(vExcelWorkbook.Sheets("Sheet1"), Excel.Worksheet)
  vExcelSheet2 = CType(vExcelWorkbook.Sheets("Sheet2"), Excel.Worksheet)

At this point, I populate some cells and read calculated data from others. In the current setup this all works well. What's different this time is that in the following code, instead of doing it one time, I loop through it several times until the calculated fields are equal to the desired results. I am using the same variable names each time through the loop so as to not create so many RCW's.... and please don't let that term fool anyone into thinking I know what I'm doing. I have a good understanding of the process but at my level of expertise I get lost very quickly.

Code:

  Do Until vExcelSheet1.Range("K6").Value >= vWord(1)
        vExcelRange1 = vExcelSheet1.Range(DimACell)
        vExcelRange1.Value = DimA
        DimA = DimA + vIncrement
  Loop

Now the only difference between this block and the other block which doesn't leave Excel running is that I don't use the loop, and the (DimACell) is actually a cell number "F5". I am assigning on the fly here because the variables being used determine whether I use cell "D5" of cell "F5". And this part seems to be working fine, in that it chooses the correct cell to work with.

What happens is that when the program is over, instead of having a Task manager clear of Excel instances, I end up with one still open. And each time this thing runs, which can be 50-60 times a day, it creates and keeps open another instance of Excel.

I have mainly just copied and pasted my other code which thanks to great assistance (and patience) from kevintheinstructor and others herein, I finally nailed down how to get all RCW's eliminated so when Excel closes down, it's all gone. Oh yes, here is the code I'm using to close down Excel when the code is completed.

Code:

        '******************************************************************************
        ' NOW SHUT DOWN EXCEL
        '******************************************************************************
        vExcelWorkbook.Close(False)
        vExcelApp.UserControl = True
        vExcelApp.Quit()

        Marshal.FinalReleaseComObject(vExcelRange1)
        Marshal.FinalReleaseComObject(vExcelSheet2)
        Marshal.FinalReleaseComObject(vExcelSheet1)
        Marshal.FinalReleaseComObject(vExcelWorkbook)
        Marshal.FinalReleaseComObject(vExcelWorkbooks)
        Marshal.FinalReleaseComObject(vExcelApp)

        vExcelRange1 = Nothing
        vExcelApp = Nothing
        vExcelWorkbooks = Nothing
        vExcelWorkbook = Nothing
        vExcelSheet1 = Nothing
        vExcelSheet2 = Nothing

        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()

I should add here that this project is basically a TCP Listener which waits until it receives a stream of data over the Internet from a commercial server at our hosting company. It processes that data and sends back the resulting calculations. The Excel portion of this project is simply another module within the project. I'm wondering if I should make two separate projects with the Excel being in it's own project. When I stop the TCP Listening process I check to see if the Excel instance is still in the task manager and it is. I have stepped through this code one line at a time and I can see that it definitely does go through all the lines which should close down Excel.

You know, I think I'll start looking into writing something which will count and track all the RCW's which get opened and when and if they get released. I could really use a tool like that now to find out which one if any of these is causing the trouble.

Viewing all articles
Browse latest Browse all 27233

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>