Results 1 to 27 of 27

Thread: Excel 2002 won't die, using Automation

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

    Excel 2002 won't die, using Automation

    Slightly complex scenario maybe, so please bear with me as I try to explain it. The actual question will be at the end, but needs an introduction.

    This is a small program I'm writing to query an SQL server, get a bunch of information, and then write to an Excel sheet and format the data a bit (stuff like certain rows being different colors, some cells in bold, a few page breaks).

    The spreadsheet I'm writing to is in a pre-made blank workbook sitting in the same directory as the program itself, that has macros in it. Initially I just wanted to write all the information, do an objExcel.visible = true, and then let the user play with it. The thing I noticed though was that there was a major slowdown with the macros contained in the spreadsheet. Things take a few seconds each now where before they took a small fraction of a second. It really isn't acceptable.

    So, then I decided to instead try to just save the spreadsheet and close excel to get it "unhooked" from the visual basic program. The problem is, the process won't die. I visited http://support.microsoft.com/default...b;en-us;317109 and tried applying that to my program. Now, when I leave the program running and create additional spreadsheets, I can watch processes spawn and then die, but the very first process always stays. It causes Excel to hang / crash when I try to open a file I'd created with the program during that session. If I kill it there seem to be no ill effects and then everything runs well, but that isn't an option unless I can be sure that I'm killing the right process. I figure there's either a glitch or I'm doing something dumb. Since I'm pretty new to programming in general, I wouldn't rule out either.

    Has anyone dealt with persisting evil excel processes and automation before?

    edit: forgot the code.

    VB Code:
    1. Private Sub cmdBuildSheet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuildSheet.Click
    2.  
    3.         Dim ExcelArr As Object(,)
    4.         Dim colsCount As Integer
    5.         Dim rowsCount As Integer
    6.         Dim objExcel As Excel.Application
    7.         Dim objWorkbooks As Excel.Workbooks
    8.         Dim objWorkbook As Excel.Workbook
    9.         Dim objSheet As Excel.Worksheet
    10.         Dim objRange As Excel.Range
    11.  
    12.         Try
    13.  
    14.             radNoWorkOrders.Enabled = False
    15.             radWorkOrders.Enabled = False
    16.  
    17.             objExcel = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    18.             objWorkbooks = objExcel.Workbooks
    19.             objWorkbook = objExcel.Workbooks.Open(Application.StartupPath & "\NOTOUCH.xls")
    20.             objSheet = DirectCast(objWorkbook.ActiveSheet, Excel.Worksheet)
    21.  
    22.             _arrIndex = 1
    23.  
    24.             If radWorkOrders.Checked = True Then
    25.  
    26.                 ReDim _PartsArr(11, 39)
    27.  
    28.                 _PartsArr(0, 1) = "Parent"
    29.                 _PartsArr(1, 1) = cboPart.Text
    30.                 _PartsArr(3, 1) = txtDescription.Text
    31.  
    32.                 GetPartsWO(cboPart.Text, 1)
    33.  
    34.                 objRange = objSheet.Range("A:D")
    35.                 objRange.NumberFormat = "@"
    36.                 objRange = objSheet.Range("E:G")
    37.                 objRange.NumberFormat = "0"
    38.                 objRange = objSheet.Range("H:H")
    39.                 objRange.NumberFormat = "@"
    40.                 objRange = objSheet.Range("I:I")
    41.                 objRange.NumberFormat = "0"
    42.                 objRange = objSheet.Range("J:K")
    43.                 objRange.NumberFormat = "@"
    44.  
    45.                 Dim iterateArr As Integer = _PartsArr.GetUpperBound(1)
    46.                 Do Until Not _PartsArr(1, iterateArr) = "" Or Not _PartsArr(9, iteratearr) = ""
    47.                     iterateArr = iterateArr - 1
    48.                 Loop
    49.                 rowsCount = iterateArr
    50.  
    51.             ElseIf radNoWorkOrders.Checked = True Then  
    52.                 ReDim _PartsArr(10, 39)
    53.  
    54.                 _PartsArr(0, 1) = "Parent"
    55.                 _PartsArr(1, 1) = cboPart.Text
    56.                 _PartsArr(3, 1) = txtDescription.Text
    57.  
    58.                 GetParts(cboPart.Text, 1)
    59.  
    60.                 objRange = objSheet.Range("A:D")
    61.                 objRange.NumberFormat = "@"
    62.                 objRange = objSheet.Range("E:E")
    63.                 objRange.NumberFormat = "0"
    64.                 objRange = objSheet.Range("F:F")
    65.                 objRange.NumberFormat = "0.00"
    66.                 objRange = objSheet.Range("G:H")
    67.                 objRange.NumberFormat = "0"
    68.                 objRange = objSheet.Range("I:I")
    69.                 objRange.NumberFormat = "@"
    70.                 objRange = objSheet.Range("J:J")
    71.                 objRange.NumberFormat = "0"
    72.  
    73.                 Dim iterateArr As Integer = _PartsArr.GetUpperBound(1)
    74.                 Do Until Not _PartsArr(1, iterateArr) = ""
    75.                     iterateArr = iterateArr - 1
    76.                 Loop
    77.                 rowsCount = iterateArr
    78.  
    79.             End If
    80.  
    81.             colsCount = _PartsArr.GetUpperBound(0)
    82.  
    83.             ReDim ExcelArr(rowsCount, colsCount)
    84.             For iterate1 As Integer = 0 To rowsCount
    85.                 For iterate2 As Integer = 0 To colsCount - 1
    86.                     ExcelArr(iterate1, iterate2) = _PartsArr(iterate2, iterate1)
    87.                 Next
    88.             Next
    89.  
    90.             Dim objPageSetup As Excel.PageSetup = objSheet.PageSetup
    91.             objPageSetup.TopMargin = 55
    92.             objPageSetup.HeaderMargin = 18
    93.             objPageSetup.BottomMargin = 0
    94.             objPageSetup.FooterMargin = 0
    95.             objPageSetup.LeftMargin = 0
    96.             objPageSetup.RightMargin = 0
    97.             objPageSetup.PrintTitleRows = "A1:K1"
    98.  
    99.             objRange = objSheet.Range("A1:A1")
    100.             objRange.Resize(rowsCount + 1, colsCount).Font.Size = 8
    101.             objRange = objSheet.Range("A2:A2")
    102.             objRange.Resize(rowsCount, colsCount).Borders.Value = 1
    103.  
    104.             objRange = objSheet.Range("A1:A1")
    105.             objRange.Resize(rowsCount + 1, colsCount + 1).Value = ExcelArr
    106.  
    107.             For iterateColors As Integer = 1 To rowsCount + 1
    108.                 Select Case _PartsArr(colsCount, iterateColors - 1)
    109.                     Case Is = "PU  "
    110.                         objRange = objSheet.Range("D" & iterateColors.ToString)
    111.                         objRange.Interior.Color = RGB(255, 153, 204)
    112.                     Case Is = "MA  "
    113.                         objRange = objSheet.Range("D" & iterateColors.ToString)
    114.                         objRange.Interior.Color = RGB(255, 255, 0)
    115.                     Case Is = "FA  "
    116.                         objRange = objSheet.Range("D" & iterateColors.ToString)
    117.                         objRange.Interior.Color = RGB(153, 204, 0)
    118.                     Case Is = "CO  "
    119.                         objRange = objSheet.Range("D" & iterateColors.ToString)
    120.                         objRange.Interior.Color = RGB(153, 204, 255)
    121.                 End Select
    122.             Next
    123.  
    124.             Dim objPageBreaks As Excel.HPageBreaks = objSheet.HPageBreaks
    125.             Dim pageFlag As Boolean = False
    126.             For iterateRows As Integer = 1 To rowsCount + 1
    127.                 If _PartsArr(0, iterateRows - 1) = "  1" Then
    128.                     objRange = objSheet.Range("A" & iterateRows.ToString)
    129.                     objRange.Resize(1, colsCount).Font.Bold = True
    130.                     If pageFlag = True Then
    131.                         objPageBreaks.Add(objSheet.Cells(iterateRows, 1))
    132.                     Else
    133.                         pageFlag = True
    134.                     End If
    135.                 End If
    136.             Next
    137.  
    138.             objRange = objSheet.Range("A1:A1")
    139.             objRange.RowHeight = 25.5
    140.  
    141.             If radWorkOrders.Checked = True Then
    142.                 objRange = objSheet.Range("F1:G1")
    143.                 objRange.WrapText = True
    144.                 objRange = objSheet.Range("F1:G1")
    145.                 objRange.ColumnWidth = 5
    146.                 objSheet.Cells.EntireColumn.AutoFit()
    147.                 objRange = objSheet.Range("H:H")
    148.                 objRange.ColumnWidth = 8
    149.             Else
    150.                 objRange = objSheet.Range("G1:H1")
    151.                 objRange.WrapText = True
    152.                 objRange = objSheet.Range("G1:H1")
    153.                 objRange.ColumnWidth = 5
    154.                 objSheet.Cells.EntireColumn.AutoFit()
    155.             End If
    156.  
    157.             objWorkbook.SaveAs(Application.StartupPath & "\" & _PartsArr(1, 1) & ".xls")
    158.  
    159.             NAR(objPageBreaks)
    160.             NAR(objPageSetup)
    161.             NAR(objRange)
    162.             NAR(objSheet)
    163.             objWorkbook.Close(False)
    164.             NAR(objWorkbook)
    165.             NAR(objWorkbooks)
    166.             objExcel.Quit()
    167.             NAR(objExcel)
    168.  
    169.             GC.Collect()
    170.             GC.WaitForPendingFinalizers()
    171.  
    172.             MessageBox.Show("Completion!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
    173.  
    174.         Catch ex As Exception
    175.             MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
    176.         Finally
    177.             radWorkOrders.Enabled = True
    178.             radNoWorkOrders.Enabled = True
    179.         End Try
    180.     End Sub
    181.  
    182.     Private Sub NAR(ByVal o As Object)
    183.         Try
    184.             System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
    185.         Catch ex As Exception
    186.             Debug.WriteLine(ex.Message)
    187.         Finally
    188.             o = Nothing
    189.         End Try
    190.     End Sub
    Last edited by RobDog888; Mar 31st, 2006 at 11:34 AM. Reason: Took smilies out of code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width