【问题标题】:Excel VSTO, Workbook Open Event Not FiringExcel VSTO,工作簿打开事件未触发
【发布时间】:2014-07-17 04:02:35
【问题描述】:

我在 VB.Net 的 Excel Addin Proect 中有以下代码:

Public Class ThisAddIn

    Private Sub Application_WorkbookOpen(Wb As Microsoft.Office.Interop.Excel.Workbook) Handles Application.WorkbookOpen
        Beep()
        MsgBox("fad")
    End Sub
End Class

这是由 VB 编辑器生成的。它是打开工作簿时的事件处理程序。当我按 F5 并运行代码时,显然事件处理程序没有执行。有什么想法吗?

编辑:如果我从打开的工作簿中打开工作簿,事件处理程序将运行,但不会为原始工作簿本身运行。

【问题讨论】:

    标签: vb.net excel vsto


    【解决方案1】:

    好吧,您知道 Excel 启动时不会调用 Open 事件,只有当您打开现有工作簿时才会调用该事件。

    有一个事件 **NewWorkbook* 有趣的是也没有触发 ...

    我找到了一种方法来处理这个问题,但不得不说我只测试了 1 分钟,试一试并告诉我们

    Public Class ThisAddIn
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            AddHandler Globals.ThisAddIn.Application.WorkbookOpen, AddressOf MyWorkbookOpenEvent
            AddHandler Globals.ThisAddIn.Application.NewWorkbook, AddressOf MyNewWorkbookEvent
            If Globals.ThisAddIn.Application.Workbooks.Count = 1 Then MyWorkbookOpenEvent(Globals.ThisAddIn.Application.Workbooks(1))
        End Sub
    
        Private Sub MyWorkbookOpenEvent(ByVal Wb As Microsoft.Office.Interop.Excel.Workbook)
            System.Windows.Forms.MessageBox.Show("OPEN workbook event")
        End Sub
    
        Private Sub MyNewWorkbookEvent(ByVal Wb As Microsoft.Office.Interop.Excel.Workbook)
            System.Windows.Forms.MessageBox.Show("NEW Workbook event")
        End Sub
    
        Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
            RemoveHandler Globals.ThisAddIn.Application.WorkbookOpen, AddressOf MyWorkbookOpenEvent
            RemoveHandler Globals.ThisAddIn.Application.NewWorkbook, AddressOf MyNewWorkbookEvent
        End Sub
    End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多