如何在加载项卸载(关闭)期间写入工作表

问题描述 投票:0回答:1

我有一个用 vb.net 编写的 Excel VSTO 插件。我想在用户卸载加载项时向活动工作簿和活动工作表写入一条消息。当我尝试写入工作表时,我得到一个

COMException HRESULT: 0x800AC472
。 我可以从工作表中成功读取。当代码运行时,加载项关闭对话框保持打开状态。互联网上有很多关于确保 Excel 可用/活动等的信息。我尝试过选择工作表、选择单元格、使用单元格而不是范围、等待 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):异常 来自 HRESULT:0x800AC472 处 Microsoft.Office.Interop.Excel._Worksheet.Select(对象替换)位于 Claude_Activesheet_1.ThisAddIn.PerformExcelOperations() 中 C:\用户\尼尔 科隆\来源 epos\Claude_Activesheet_1\ThisAddIn.vb:第 31 行

删除第 31 行

activesheet.Select()
只是将错误移至 activeSheet.Cells....... 行。

excel vsto add-in shutdown
1个回答
0
投票

您可能想检查在插件关闭期间工作簿/工作表是否仍然处于活动状态(不是什么都没有)。可能存在一些计时问题?

另一个选项可能是通过代码关闭插件(因此在插件功能区中创建一个按钮,首先执行 Excel 操作,然后关闭插件。 将 yourAddIn 调暗为 Excel.AddIn = this.Application.AddIns("AddIn") yourAddIn.installaed=false

今天早上我会尝试一下,这是一个有趣的😊。

© www.soinside.com 2019 - 2024. All rights reserved.