56408

InvalidCOMObjectException: COM object that has been separated from its underlying RCW cannot be used

I'm getting this exception thrown when I run my tests:

Test method OuvertureClasseur threw exception: System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used...

though each of my test passes when ran individually.

Here's my tests code:

<TestClass()> _ Public Class FabriqueExcelTests Private Shared _xl As ApplicationClass <ClassInitialize()> _ Public Shared Sub MyClassInitialize(ByVal testContext As TestContext) _xl = New ApplicationClass() _xl.Visible = False _xl.ScreenUpdating = False _xl.DisplayAlerts = False End Sub <ClassCleanup()> _ Public Shared Sub MyClassCleanup() _xl.Quit() _xl = Nothing End Sub <TestMethod()> _ Public Sub ConstructeurParDefaut() Dim gestionnaire As GestionnaireExcel = New GestionnaireExcel() Assert.IsNotNull(gestionnaire) Assert.IsInstanceOfType(gestionnaire, GetType(GestionnaireExcel)) End Sub <TestMethod()> Public Sub CreationDUnFichierExcel() Dim fichier As String = "C:\Temp\CreationFichierExcel.xls" Using xl As GestionnaireExcel = New GestionnaireExcel() Dim classeur As Workbook = xl.CreerClasseur(fichier) classeur.Close() classeur = Nothing End Using Assert.IsTrue(File.Exists(fichier)) File.Delete(fichier) End Sub <TestMethod()> _ Public Sub OuvertureClasseur() Dim fichier As String = "C:\Temp\OuvertureClasseur.xls" _xl.Workbooks.Add(XlWBATemplate.xlWBATWorksheet) _xl.ActiveWorkbook.SaveAs(fichier) _xl.ActiveWorkbook.Saved = True _xl.Workbooks.Close() Using xl As GestionnaireExcel = New GestionnaireExcel() Dim classeur As Workbook = xl.OuvrirClasseur(fichier) Assert.AreEqual(fichier, classeur.FullName) classeur.Close() classeur = Nothing End Using File.Delete(fichier) End Sub End Class

And my GestionnaireExcel class:

Public Class GestionnaireExcel Implements IDisposable Private _cultureOriginale As CultureInfo Private _disposedValue As Boolean = False '' To detect redundant calls Private _excel As ApplicationClass Public Sub New() InitialiserExcel() End Sub '' IDisposable Protected Overridable Sub Dispose(ByVal disposing As Boolean) If Not Me._disposedValue Then If disposing Then '' TODO: free other state (managed objects). If (_excel IsNot Nothing) Then _excel.Quit() _excel = Nothing System.Threading.Thread.CurrentThread.CurrentCulture = _cultureOriginale End If End If '' TODO: free your own state (unmanaged objects). '' TODO: set large fields to null. End If Me._disposedValue = True End Sub '' This code added by Visual Basic to correctly implement the disposable pattern. Public Sub Dispose() Implements IDisposable.Dispose '' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(True) GC.SuppressFinalize(Me) End Sub Public Function CreerClasseur(ByVal classeur As String) As Workbook _excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet) _excel.ActiveWorkbook.SaveAs(classeur) Return _excel.ActiveWorkbook End Function Private Sub InitialiserExcel() _cultureOriginale = System.Threading.Thread.CurrentThread.CurrentCulture System.Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US") _excel = New ApplicationClass() _excel.DisplayAlerts = False _excel.ScreenUpdating = False _excel.Visible = False End Sub Public Function OuvrirClasseur(ByVal fichier As String) As Workbook Dim classeur As Workbook = _excel.Workbooks.Open(fichier _ , False, False _ , Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing _ , False _ , Type.Missing, Type.Missing _ , False _ , Type.Missing, Type.Missing) Return classeur End Function End Class

<strong>Related questions found</strong>

Though I have read and understand these questions and answers, I don't seem to find what causes my OuvertureClasseur() test to fail when I run all my tests together, and it works just fine when I run it individually.

<ol> <li>COM object that has been separated from its underlying RCW cannot be used.</li> <li>WMI Exception: “COM object that has been separated from its underlying RCW cannot be used”</li> <li>COM object that has been separated from its underlying RCW cannot be used.</li> <li>COM object that has been separated from its underlying RCW can not be used - why does it happen?</li> <li>WPF Thread: “COM object that has been separated from its underlying RCW cannot be used.”</li> </ol>

I have been running around for few hours now, and any help will gladly be appreciated.

Thanks in advance! =)

Answer1:

Excel's executing as another process. It's possible that what's happening in classeur.Close() is asynchronous, and that some of it is taking place after the GestionnaireExcel instance is disposed.

Try commenting out the using block and see what happens. Or if the Excel COM object exposes a property saying whether the app is still alive, try putting in a loop to monitor that and not leave your using block until you're sure it's gone.

This is just a guess, but I see nobody else has answered yet, so I'll toss it out there.

Recommend

  • Conditional IO action
  • Return the choice of a combobox javafx
  • Primary key with ASC or DESC ordering?
  • issue for received image in php with upload script Android
  • F# Excel UsedRange is not Defined
  • perl - put the value of array in a array
  • Write and execute excel VB macro with win32com.client
  • how read excel file having more than 100000 row in java?
  • Matlab: changing font size on xlabel and ylabel
  • How to pivot a dataframe with pandas so variable columns become rows?
  • jsrender nested templates with string markup
  • How can i make the app accepted by all screen resolution [duplicate]
  • Geocoder.getFromLocation grpc failed on Android real device
  • How to calculate/generate a string with all possible values in an array?
  • In C# how to access excel headers using OLEDB (without Automation)?
  • VBA ADO Excel 2010
  • Android Studio 3.0 Canary 1 - Simulator stuck on boot
  • Detecting and replacing LSEP character (&#xB;)
  • InvalidCOMObjectException: COM object that has been separated from its underlying RCW cannot be used
  • How do I export charts from Excel as graphics
  • Bootstrap 4 - Sticky Navbar with fixed margin
  • Ruby Multiple OR Evaluations For String Value
  • Send data to WPF singleton application from other process
  • Copying and renaming unopened workbook in excel
  • Get picture position in Apache POI from Excel xls HSSF
  • how to insert new rows with values in the same sheet of an excel file in java
  • Removing levels from data frame read from CSV file - R
  • jQuery random blockquote
  • View/Download Pdf Files in React - Router 4
  • NSIS decompiler
  • How to 'create temp table as select' in Slick?
  • How load cv2.KeyPoint and Descriptors correctly on OpenCV 3 with Python 3?
  • Paperclip, set path outside of rails root folder
  • TFS: Get latest causes slow project reloading
  • Running a C# exe file
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • Reading document lines to the user (python)