You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VSTO加载项卸载时写入工作表遇COMException(0x800AC472)求助

Excel VSTO加载项卸载时写入工作表触发COMException(0x800AC472)问题

使用VB.NET开发Excel VSTO加载项,需求是在用户卸载加载项时,向活动工作簿的活动工作表写入一条消息。但执行写入操作时会触发System.Runtime.InteropServices.COMException (0x800AC472)异常,读取工作表内容的操作却能正常执行。加载项关闭对话框会在代码运行期间保持打开状态。

已尝试以下方案但均未解决问题:

  • 选择工作表、选择单元格
  • 使用Cells而非Range
  • 等待Excel就绪并设置不同时长重试

最新代码

Imports System.Diagnostics

Public Class ThisAddIn

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        Debug.WriteLine("addin loaded")
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
        PerformExcelOperations()
    End Sub
    Private Sub PerformExcelOperations()
        Dim activeBook As Excel.Workbook = TryCast(Globals.ThisAddIn.Application.ActiveWorkbook, Excel.Workbook)
        Dim activeSheet As Excel.Worksheet = TryCast(Globals.ThisAddIn.Application.ActiveSheet, Excel.Worksheet)
        Dim success As Boolean = False
        Dim counter As Integer
        Dim sleep As Integer = 1000
        If activeBook IsNot Nothing Then
            Debug.WriteLine("this workbook is " & activeBook.Name)
        End If

        If activeSheet IsNot Nothing Then
            Dim sheetName As String = activeSheet.Name
            Debug.WriteLine("Active sheet name: " & sheetName)
            'While Not success
            While success = False
                Try
                    ' Write to cell A1
                    'If activeSheet.Application.Ready Then
                    If activeSheet.Application.Interactive = True Then
                        activeSheet.Select()
                        activeSheet.Cells(1, 1).value2 = "writing to cell A1"
                        Debug.WriteLine("Value written to cell A1")
                        success = True
                    Else
                        Debug.WriteLine("app Not ready")
                        counter += 1
                    End If
                Catch ex As Exception
                    Debug.WriteLine(ex.ToString)
                    System.Threading.Thread.Sleep(1000) ' Wait for 1 second before retrying.
                    counter += 1
                    Debug.WriteLine("counter = " & counter)

                End Try
                If counter > 10 Then
                    Debug.WriteLine("exiting sub")
                    Exit Sub
                End If

                System.Threading.Thread.Sleep(sleep) ' Wait for 1 second before retrying.
                sleep = sleep - 95
                'Else
                'Debug.WriteLine("No active worksheet found")
            End While
            ' Read from cell A2
            Dim mytext As String = activeSheet.Range("A2").Value2
            Debug.WriteLine("text at A2 is " & mytext)
        End If
    End Sub
End Class

报错信息

System.Runtime.InteropServices.COMException (0x800AC472): Exception from HRESULT: 0x800AC472
at Microsoft.Office.Interop.Excel._Worksheet.Select(Object Replace)
at Claude_Activesheet_1.ThisAddIn.PerformExcelOperations() in C:\Users\Neil Keron\source\repos\Claude_Activesheet_1\ThisAddIn.vb:line 31

移除第31行的activeSheet.Select()后,错误会转移到activeSheet.Cells相关代码行。

内容的提问来源于stack exchange,提问作者NeilK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 20:04:57