Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:48:57+00:00 2026-05-20T07:48:57+00:00

I’m getting this exception thrown when I run my tests: Test method OuvertureClasseur threw

  • 0

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

Related questions found

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.

  1. COM object that has been separated from its underlying RCW cannot be used.
  2. WMI Exception: “COM object that has been separated from its underlying RCW cannot be used”
  3. COM object that has been separated from its underlying RCW cannot be used.
  4. COM object that has been separated from its underlying RCW can not be used – why does it happen?
  5. WPF Thread: “COM object that has been separated from its underlying RCW cannot be used.”

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

Thanks in advance! =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:48:57+00:00Added an answer on May 20, 2026 at 7:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.