Results 1 to 27 of 27

Thread: Excel 2002 won't die, using Automation

  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

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    Welcome to the Forums.

    You can use this on the excel application object you have created to explicitly destroy your excel object. Usually this is from some issue in your code where yor referencing excel default objects and excel instanciated objects.

    VB Code:
    1. 'oApp = Excel.Applicaiton
    2. System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

    Re: Excel 2002 won't die, using Automation

    I think that's what I'm doing. It also interests me how the subsequent processes die like they're supposed to, and only the first sticks around.

    Unless it's something more like, the first process is the application process and the others are "workbook processes" or something like that.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    Ah, ok I see your code now. This issue may be that your passing the object ByVal and not the default ByRef. When passed as ByVal it passes a copy of the object that will not update its changes to the original passed in object. so you are not really releasing the object.
    VB Code:
    1. Private Sub NAR([b]ByVal[/b] o As Object)
    2.  
    3. 'Should be
    4. Private Sub NAR(ByRef o As Object)
    5. 'Or
    6. Private Sub NAR(o As Object)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

    Re: Excel 2002 won't die, using Automation

    Excel's still going after I changed that. Do you think I might need to explicitly declare and then release *every* excel property/method/etc that I use? I thought I was already kind of going a little overboard.

    Thanks for the speedy response, by the way.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    Yes, before you posted your code I didnt know you werent doing that.
    You always need to explicitly close and destory your COM objects.

    VB Code:
    1. objWorkbooks = Nothing
    2. objWorkbook.Close
    3. objWorkbook = Nothing
    4. objSheet = Nothing
    5. objRange = Nothing
    6. objPageBreaks = Nothing
    7.  
    8. objExcel.Quit
    9. objExcel = Nothing
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

    Re: Excel 2002 won't die, using Automation

    setting the code to nothing seems to have the same effect as using System.Runtime.InteropServices.Marshal.ReleaseComObject, which is to have the initial excel process stick around until the program is terminated, but the excel.exe processes from subsequenet clicking of the button terminate properly

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    The reason why Excel "hangs" is because it has an object variable still left instanciated. So always .Close, .Quit, and = Nothing is a way of insuring that the ReleaseComObject method weill succeed.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    You should also remove the objWorkbooks object since your not using it anywhere in the procedure.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

    Re: Excel 2002 won't die, using Automation

    Then, I think the question might be "what Excel object am I leaving lying around instanced?" I think it's related to the Excel application, and not to the workbook or worksheet or anything, because the additional processes for additional workbooks and whatnot terminate properly.

    I took out the workbooks object. Heheh. oops.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    I dont see anything else that may be holding up the Excel process. Could you possibly have other Excel code somewhere else in your application?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

    Re: Excel 2002 won't die, using Automation

    Ahh, it wouldn't be that early-on directcast would it? actually, there are two of them. I don't know what to change the second one to though, since objworkbook.activesheet is an Object, and objworkbook.sheets(1) is also an Object
    VB Code:
    1. objExcel = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    2.             objWorkbook = objExcel.Workbooks.Open(Application.StartupPath & "\NOTOUCH.xls")
    3.             objSheet = DirectCast(objWorkbook.ActiveSheet, Excel.Worksheet)
    to
    VB Code:
    1. objExcel = New Excel.Application
    2.             objWorkbook = objExcel.Workbooks.Open(Application.StartupPath & "\NOTOUCH.xls")
    3.             objSheet = m00?

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    No, as DirectCast is only an explicit CType.

    Using Excel properties like ActiveSheet and ActiveWorkbook could be causing an issue with the processing hanging but shouldnt be creating a second instance of Excel in the beginning.

    Can you step through your code and have your taskmanager open to monitor when each Excel process is created?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

    Re: Excel 2002 won't die, using Automation

    I'm sorry. What I meant was that for a given session the program is run, that button that that sub belongs to may be clicked more than once, to generate more than one report. And only the process from the first click gets "stuck"

    The processing hanging was my major problem. I don't mind there being multiple processes otherwise, though, if it was closing correctlythe first time there wouldn't be, of course.

    What can I use instead of ActiveWorksheet?

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    Workbooks(1).Sheets(1)
    'Or
    Workbooks("Book1").Sheets("sheet1") or what ever the names are in your workbook.

    What I usually do is create a class level excel application object. Then upon a button click I would just open or add a new workbook to it. this will improve performance since you are not creating new Excel applicaiton instances each time you press the button.
    Then you will have a reference to only one excel applicaiton object variable. When you r appplication closes you also close and destroy your excel application variable object.

    You will need to handle multple workbook instances in the workbooks collection upon closing but if your not saving each one then its not a problem.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

    Re: Excel 2002 won't die, using Automation

    the editor still complains that those are of type "object" -- would it be appropriate to use a directcast then?

    also, there are no other bits of code related to excel in the program. If you're hit with any flashes of inspiration, I'll watch the thread. Otherwise, thanks very much for the help and I'll keep reading

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    Yes, if your using Option Strict On then you will need the DirectCast or CType.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: Excel 2002 won't die, using Automation

    I have the same problem. No matter what I do, Excel won't close.

    I wrote an example that will keep Excel open...every time. Type the filename of an xls file, press the button, and Excel will stay open...even though all objects are dereferenced in the correct way.

    VB Code:
    1. Imports Microsoft.Office.Interop
    2. Imports Microsoft.Office.Core
    3. Imports System.Windows.Forms
    4. Imports System.Drawing
    5. Imports System
    6.  
    7. Namespace test_excel
    8.     Public Class MainForm
    9.         Inherits System.Windows.Forms.Form
    10.         Private button1 As System.Windows.Forms.Button
    11.         Private textBox1 As System.Windows.Forms.TextBox
    12.        
    13.         Public Shared Sub Main
    14.             Dim fMainForm As New MainForm
    15.             fMainForm.ShowDialog()
    16.         End Sub
    17.        
    18.         Public Sub New()
    19.             MyBase.New
    20.             '
    21.             ' The Me.InitializeComponent call is required for Windows Forms designer support.
    22.             '
    23.             Me.InitializeComponent
    24.         End Sub
    25.        
    26.         #Region " Windows Forms Designer generated code "
    27.         ' This method is required for Windows Forms designer support.
    28.         ' Do not change the method contents inside the source code editor. The Forms designer might
    29.         ' not be able to load this method if it was changed manually.
    30.         Private Sub InitializeComponent()
    31.             Me.textBox1 = New System.Windows.Forms.TextBox
    32.             Me.button1 = New System.Windows.Forms.Button
    33.             Me.SuspendLayout
    34.             '
    35.             'textBox1
    36.             '
    37.             Me.textBox1.Location = New System.Drawing.Point(8, 8)
    38.             Me.textBox1.Name = "textBox1"
    39.             Me.textBox1.Size = New System.Drawing.Size(192, 20)
    40.             Me.textBox1.TabIndex = 1
    41.             Me.textBox1.Text = ""
    42.             '
    43.             'button1
    44.             '
    45.             Me.button1.Location = New System.Drawing.Point(208, 8)
    46.             Me.button1.Name = "button1"
    47.             Me.button1.TabIndex = 0
    48.             Me.button1.Text = "Try It"
    49.             AddHandler Me.button1.Click, AddressOf Me.Button1Click
    50.             '
    51.             'MainForm
    52.             '
    53.             Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    54.             Me.ClientSize = New System.Drawing.Size(292, 37)
    55.             Me.Controls.Add(Me.textBox1)
    56.             Me.Controls.Add(Me.button1)
    57.             Me.Name = "MainForm"
    58.             Me.Text = "MainForm"
    59.             Me.ResumeLayout(false)
    60.         End Sub
    61.         #End Region
    62.        
    63.         Public Function Excel_Still_Runs(ByVal FileName As String)
    64.             Dim xlsApp As New Excel.Application()
    65.             Dim xlsWorkbooks As Excel.Workbooks
    66.             Dim xlsWorkbook As Excel.Workbook
    67.            
    68.             ' Open the workbook
    69.             xlsWorkbooks = xlsApp.Workbooks
    70.             xlsWorkbook = xlsWorkBooks.Open(FileName)
    71.            
    72.             ' Close and release the workbook
    73.             xlsWorkbook.Close(False)
    74.             System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkbook)
    75.             xlsWorkbook = Nothing
    76.            
    77.             ' Release the workbooks collection
    78.             System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkbooks)
    79.             xlsWorkbooks = Nothing
    80.            
    81.             ' Issue the quit function...even though we know it won't work
    82.             xlsApp.Quit()
    83.            
    84.             ' Release the application
    85.             System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp)
    86.             xlsApp = Nothing
    87.         End Function
    88.        
    89.         Private Sub Button1Click(sender As System.Object, e As System.EventArgs)
    90.             Excel_Still_Runs(Me.textBox1.Text)
    91.         End Sub
    92.     End Class
    93. End Namespace

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    Are you creating 2 instances of your form? What is your startup object? What version of VB are you running? Also, you should set the objects = to nothing before releasing the com object.

    Another point is that you have an unnecessary Imports of Office.Core when all you need is the Office.Interop.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  20. #20
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: Excel 2002 won't die, using Automation

    No...one instance of the form. I use SharpDevelop for my IDE...which creates the shared main function to instantiate the form...which that shouldn't matter. I could just as well do the exact same thing with Excel using a console window.

    The code is written for .NET 1.1, but I have also did the same code for .NET 1.0, and the same thing happens.

    Now, If I set the objects to nothing before releasing the com object, how would it know what to release? Plus, Microsoft seems to agree with the way I'm doing it (look at the NAR() function for C#): http://support.microsoft.com/default.../317109/EN-US/

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    Did you remove the imports of the Office.Core ?

    I disagree with that ms example as I have seen it before. They are passing the objects BrVal and trying to destroy and release the copy and not the actual reference as if they passed it ByRef instead.

    For your issue may be a timing issue since you are creating/opening line after line. What if you create in one button click and in another you destroy/release?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  22. #22
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: Excel 2002 won't die, using Automation

    Office.Core is in the code I'm actually working with. I had forgotten to remove it from the sample. But it doesn't matter if it's there or not...the error remains.

    As for passing by reference, as you can tell in my example code...there is no passing by value, except for the filename, as I release the COM objects inline.

    Plus, what is the point of automating a process if I have to click buttons in-between? Kinda defeats the purpose.

    If you have example code that actually closes Excel properly, I would love to see it.

    Sorry if I seem a bit agitated...but I have been dealing with this problem for the past year. The only option I found that somewhat works is to kill the process by executing taskkill using the shell command.

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    I understand the frustration as others have the same issues with killing office apps from .net.

    The ByRef I was refering to was in the MS example.

    The multiple button clicks was just for testing purposes to try to isolate any possible issues.

    From what I understand, Office creates a wrapper for an office app and its this wrapper that retains the process until all references to office objects are released and destroyed.

    I tried your exact code in a test project and added a Sleep line so I could see Excel appear in the task processes list and it ran correctly and destroyed excel correctly.

    Do you have the PIAs installed?

    Which version of VB are you using?

    Is your Office updated with the lasted Office Update Online?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  24. #24
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Excel 2002 won't die, using Automation

    Quote Originally Posted by l33t_c0w
    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.
    Here is how you can kill the right process.

    First, after you started the Excel process that you want to kill (the first one I think) call this function

    VB Code:
    1. Declare Function GetCurrentProcessId Lib "kernel32" () As Long

    This function return the process ID of the Excel process just created.
    Put the result in a variable

    Once you have done everything you want to do and want to kill the god dam process

    do

    VB Code:
    1. Function KillPId()
    2.  
    3.         Set ProcessList = GetObject("winmgmts://.").InstancesOf("win32_process")
    4.  
    5.     For Each Process In ProcessList
    6.         If Process.ProcessID = variable Then ' variable = the processID obtain by the GetCurrentProcessId function
    7.                           Process.Terminate
    8.         End If
    9.     Next
    10.  
    11.     End Function

    Hope it will helps a bit
    Last edited by billhuard; Apr 5th, 2006 at 09:37 AM.

  25. #25
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: Excel 2002 won't die, using Automation

    RobDog888,

    That is strange that it works for you but not for me...I just tried sleeping after every command to see if it was Excel that was loosing it's mind...but it still does the same thing.

    I'm using VB.NET 1.1 and using SharpDevelop 1.1 for my IDE.

    Also, I'm working with Office 2003 SP2, and I'm using the Office XP PIA's (in a folder and not installed in the GAC). I also thought that the version of PIA's that I was using could be causing the issue...so I just changed the PIA's in my project to the 2003 version (which was installed in the GAC)...still does the same thing.

    -----

    billhuard,

    Your solution works...that is essentially what I'm currently using. But where the difference in my code is...I'm not killing the process from a macro, but rather I'm killing Excel from my .NET Application. I use a function called "GetWindowThreadProcessID" which is in user32.dll to get the process ID by providing the window handle to the specific Excel application (from Application.Hwnd). Then, I just feed that process ID into the taskkill command, and it kills the task.

    But, I've also had it to where the process ID could not be found, and it killed just enough processes on my computer to get it to the point that it wouldn't shut down...lol. Also, if a user opens an Excel file during the import process, I've also had it to where the file will open up with the Excel process I'm trying to kill. That is why I'm looking for the "correct" way to do it.

  26. #26
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel 2002 won't die, using Automation

    Maybe its a #Develop issue? I am running 2003 1.1 and Office 2003 SP2 with 2003 PIAs.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  27. #27
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: Excel 2002 won't die, using Automation

    I have also had this issue occur with VS 2002 (.NET 1.0) using Office XP and the XP PIAs. For right now, I found a temporary solution. I switched over to Koogra, which can read Excel files without using Excel.

    http://sourceforge.net/projects/koogra

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