none
Worsheet Selection causes RCW errors RRS feed

  • Question

  • I have a document level project that throws an error when exiting Excel (or closing the document).  I have narrowed down the common error: COM object that has been separated from its underlying RCW cannot be used to a single section of code, but I don't understand the cause of the problem.

    In my initialisation script I have the following code:

    Dim Cover As Excel.Worksheet = Globals.Sheet2.InnerObject
    Cover.Activate() 'Activate the cover sheet                                   
    Marshal.ReleaseComObject(Cover)

    Commenting out the three lines prevents the error.  Activating directly (using Globals.Sheet2.Activate), or commenting out the ReleaseComObject line both still result in the error.

    Notably, the following code does not cause a crash:

    Globals.ThisWorkbook.Worksheets("Sheet2").Activate

    But this does:

    Globals.ThisWorkbook.Worksheets(Globals.Sheet2.Name).Activate

    The worksheet does not have any OnActivate code.

    Also, note that my Init script is being called as such:

    Dim Disp As Dispatcher = Dispatcher.CurrentDispatcher		
    Dim _thread = New Thread(Sub(ExcelDisp As Dispatcher) _threadWrapper = ThreadWrapper.Instance _threadWrapper.ExcelDispatcher = ExcelDisp _threadWrapper.init() Dispatcher.Run() End Sub) _thread.SetApartmentState(ApartmentState.STA) _thread.Start(Disp)
    Any ideas on what is causing the error?



    • Edited by QuinRiva Thursday, January 22, 2015 3:53 AM
    Thursday, January 22, 2015 3:51 AM

All replies

  • I have just noticed that the Marshal.ReleaseComObject(Cover) is actually throwing an error:
    Failed to release object: COM object that has been separated from its underlying RCW cannot be used.

    This would explain the crash when closing, but I'm unsure as to why this error is being thrown here.

    Thursday, January 22, 2015 4:24 AM
  • Do you run the problematic code on another thread? Do you get any exception using the single thread?
    Thursday, January 22, 2015 3:12 PM
  • Do you run the problematic code on another thread? Do you get any exception using the single thread?

    I have tried running the code on in the main thread:

    Private Sub ThisWorkbook_Startup() Handles Me.Startup
    Dim Cover As Excel.Worksheet = Globals.Sheet2.InnerObject
    Cover.Activate() 'Activate the cover sheet
    Marshal.ReleaseComObject(Cover)
    End Sub

    but get the same error.  Note that activating the sheet does work, but as soon as I try to release the worksheet object, I crash with Failed to release COM Object.  This happens whether or not I have the Cover.Activate code in there.


    Thursday, January 22, 2015 10:51 PM
  • As a test, I started a completely new project (Excel Template).  I get the same error occurring:

    Public Class ThisWorkbook
        Private Sub ThisWorkbook_Startup() Handles Me.Startup
            Dim Cover As Excel.Worksheet = Globals.Sheet1.InnerObject
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Cover)  ' <-- Crashes here
        End Sub
    
        Private Sub ThisWorkbook_Shutdown() Handles Me.Shutdown
            
        End Sub
    End Class

    This is the only code in the entire project and I still get the error.  From my understanding, I have created COM Object which I must then release using Marshal.ReleaseComObject.  I don't understand how this is different to releasing other COM Objects:

    Dim rngA1 As Excel.Range = Globals.Sheet1.Range("A1")
    System.Runtime.InteropServices.Marshal.ReleaseComObject(rngA1)
    ^ This works without any problems by the way.


    Thursday, January 22, 2015 11:10 PM
  • Hi QuinRiva,

    I am trying to reproduce the issue howeve faild. Here are the test steps:
    1. Create a Excel Template project

    2. add a new sheet(Sheet2)

    3. Modify the Startup event like code below:

     Private Sub ThisWorkbook_Startup() Handles Me.Startup
            Dim Cover As Excel.Worksheet = Globals.Sheet1.InnerObject
            Cover.Activate() 'Activate the cover sheet
            Marshal.ReleaseComObject(Cover)
    
        End Sub

    4. Run the project, the code works well for me

    Did I misunderstand? Also I suggest that you try to repair or update the Office to latest version to see whether the issue was fixed.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 23, 2015 8:30 AM
    Moderator