Excel VSTO,工作簿Open Event Not fired

时间:2021-01-22 20:42:46

I've got the code below in an Excel Addin Proect in VB.Net:

我在VB.Net的Excel插件中有如下代码:

Public Class ThisAddIn

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

This was generated by the VB editor. It is the event handler for when the workbook is opened. When I press F5 and run the code, apparently the event handler doesn't execute. Any Ideas?

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

Edit: The event handler will run if I open a workbook from the workbook that opens, but will not run for the original workbook itself.

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

1 个解决方案

#1


1  

Well, you know the Open event is not called when Excel starts, the event is called only when you open an existing workbook.

您知道,当Excel开始时不会调用Open事件,只有在打开一个现有的工作簿时才会调用这个事件。

There is an event **NewWorkbook* which interestingly enough is not fired either ...

有一个事件**新闻手册*有趣的是它也没有被解雇……

I found a way how to handle with this but have to say I tested only for 1 minute, give it a try and let us know

我找到了处理这个问题的方法,但不得不说我只测试了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

#1


1  

Well, you know the Open event is not called when Excel starts, the event is called only when you open an existing workbook.

您知道,当Excel开始时不会调用Open事件,只有在打开一个现有的工作簿时才会调用这个事件。

There is an event **NewWorkbook* which interestingly enough is not fired either ...

有一个事件**新闻手册*有趣的是它也没有被解雇……

I found a way how to handle with this but have to say I tested only for 1 minute, give it a try and let us know

我找到了处理这个问题的方法,但不得不说我只测试了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