dcsimg
Results 1 to 6 of 6

Thread: Excel hangs in task manager

  1. #1

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,440

    Excel hangs in task manager

    I'm feeling pretty stupid right now. There must be thousands of links on Excel hanging up and I've tried many of them but I just can't get it to close out of the task manager. This is the code:

    Code:
    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private xl_In As Excel.Application
        Private xlWorkBook_In As Excel.Workbook
        Private xlWorksheet_In As Excel.Worksheet
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            xl_In = New Excel.Application
            xl_In.Application.Workbooks.Open("C:\WA Claim ten rows.xlsx")
    
            xl_In.DisplayAlerts = False
            xl_In.Application.ActiveWorkbook.SaveAs("C:\WA Claim ten rowsErors.xlsx")
            xl_In.Workbooks.Close()
            xl_In.Quit()
    
            xlWorksheet_In = Nothing
            xlWorkBook_In = Nothing
            xl_In.Quit()
            xl_In = Nothing
    
        End Sub
    
    End Class
    This is one of a few tries but I want to open an existing spreadsheet, save it as another name, and close it out. Help would be appreciated.
    Please remember next time...elections matter!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,144

    Re: Excel hangs in task manager

    try this:

    Code:
    xl_In.Quit()
    Marshal.ReleaseComObject(xl_In)
    xl_In = Nothing

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,415

    Re: Excel hangs in task manager

    xl_In is an Application object by itself, so there is nothing to gain from using xl_In.Application , but using it could cause problems.

    Using any Active* objects is dodgy, and should be avoided wherever possible... in this case by simply storing the workbook you open, and using the variable.

    Try this modified version (probably in addition to .paul.'s suggestion):
    Code:
    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private xl_In As Excel.Application
        Private xlWorkBook_In As Excel.Workbook
        Private xlWorksheet_In As Excel.Worksheet
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            xl_In = New Excel.Application
            xlWorkBook_In = xl_In.Workbooks.Open("C:\WA Claim ten rows.xlsx")
    
            xl_In.DisplayAlerts = False
            xlWorkBook_In.SaveAs("C:\WA Claim ten rowsErors.xlsx")
            xlWorkBook_In.Close()
    
            xlWorksheet_In = Nothing
            xlWorkBook_In = Nothing
            xl_In.Quit()
            xl_In = Nothing
    
        End Sub
    
    End Class
    Note that setting DisplayAlerts is a bit dodgy, as sometimes the setting is remembered when the user opens Excel manually (which could lead to them not getting a "save changes?" message or similar). I'd recommend setting it to True at the end of your code (or returning it to the previous state).

  4. #4
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Excel hangs in task manager

    I found this advice from TnTinMN very helpful in not having to deal with the stupid "2 dot" rule.

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Excel hangs in task manager

    Can you clarify the duration of "Excel hangs in task manager". Is it only while your application is running, or does it remain after your code exits? Also, is this an issue when running without the debugger attached?

  6. #6

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,440

    Re: Excel hangs in task manager

    Thanks everyone for your responses...I have to shift gears for a while. Just as a work around instead of saving one sheet to a new name I used a file copy. I hope to come back to this just to understand the issue but I can't today. Thanks for the responses to date.
    Please remember next time...elections matter!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width