运行测试时抛出此异常:
Test method
OuvertureClasseur
threw exception: System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used...
虽然我的每个测试在单独运行时都通过了。
这是我的测试代码:
<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
还有我的 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
虽然我已经阅读并理解了这些问题和答案,但当我一起运行所有测试时,我似乎没有找到导致我的 OuvertureClasseur()
测试失败的原因,而且它工作得很好当我单独运行它时。
- COM object that has been separated from its underlying RCW cannot be used.
- WMI Exception: “COM object that has been separated from its underlying RCW cannot be used”
- COM object that has been separated from its underlying RCW cannot be used.
- COM object that has been separated from its underlying RCW can not be used - why does it happen?
- WPF Thread: “COM object that has been separated from its underlying RCW cannot be used.”
我已经跑了几个小时了,我们将很高兴提供任何帮助。
提前致谢! =)
最佳答案
Excel 作为另一个进程执行。 classeur.Close() 中发生的事情可能是异步的,并且其中一些发生在 GestionnaireExcel 实例被处置之后。
尝试注释掉 using block ,看看会发生什么。
关于visual-studio - 无效COMObjectException : COM object that has been separated from its underlying RCW cannot be used?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5109065/