1

I'm using VB.net in MS Visual Studio 2012 to create an add-in for Excel (2010, 2007). The add-in uses Excel's Styles to format reports. Rather than iterate through the Styles collection, I thought it would be easier to try getting a reference to my style by name and catch the exception if it doesn't exist.

Imports xi = Microsoft.Office.Interop.Excel

<ComVisible(True)> _
<ClassInterface(ClassInterfaceType.None)> _
Public Class ReportOptions
    Implements IReportOptions

    Private Const _TitleStyleName As String = "TitleStyle"
    Private Const _SubtitleStyleName As String = "SubtitleStyle"

    Public Sub SetDefaults() Implements IReportOptions.SetDefaults
        Dim MyApp As xi.Application
        Dim wb As xi.Workbook
        Dim styles As xi.Styles
        Dim SubtitleStyle As xi.Style
        Dim TitleStyle As xi.Style

        MyApp = GetObject(, "Excel.Application")
        wb = MyApp.ActiveWorkbook
        styles = wb.Styles
        Try
            SubtitleStyle = styles.Item(_SubtitleStyleName) 'Exception here
        Catch ex As COMException
            SubtitleStyle = styles.Add(_SubtitleStyleName)
        End Try

        TitleStyle.Font.Name = "Calibri"
        'More code setting style values

        'Code to clean up the COM Objects...
    End Sub
End Class

I keep getting Visual Studio's exception dialog for "COMException crossed a native/managed boundary" with the following details:

Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.Styles.get_Item(Object Index)
at ReportClassLibrary.ReportClassLibrary.ReportOptions.SetDefaults() in <Path>\ReportClassLibrary.vb:line 68

I have tried everything I could think of to catch this "simple" exception:

  1. the infamous catch-all "Catch ex As Exception", or "Catch"
  2. Googled various combinations of COMException, DISP_E_BADINDEX, workbook.styles, and several forms of "can't catch exception"
  3. verified the Thrown checkbox is NOT checked for COMException under Common Language Runtime Exceptions -> System.Runtime.InteropServices
  4. treating it as a Corrupt State Exception (CSE) by adding the HandleProcessCorruptedStateExceptions Attribute
  5. Checked Enable native code debugging in the Debug section of the Project Properties

I realize it may be better (and possibly even faster) to loop through entire Styles collection looking for a match to _TitleStyleName, but I would much prefer to understand why I've been unable to simply Catch this Exception.

Thank you for taking the time to read all this. :)

4

1 回答 1

1

我知道它必须是简单的!

我打算按照 Hans Passant 的建议仔细检查 Exception 设置,但乍一看我没有在Debug menu上看到 Exceptions 选项。相反,我在底部附近看到了选项和设置。这带来了选项对话框。在Debugging -> General部分,我发现Break when exceptions cross AppDomain or managed/native boundary

我取消选中该框,现在我的 try/catch 块按预期处理 COMException!

总结
如果您在捕获异常时遇到问题,请查看它是否跨越了 AppDomain 或托管/本机边界。如果是这样:
1) 转到调试菜单
2) 单击选项和设置
3) 转到调试 -> 常规部分
4)当异常跨越 AppDomain 或托管/本机边界时取消选中中断

于 2013-03-11T02:21:50.420 回答