Excel VSTO,工作簿打开事件未触发 [英] Excel VSTO, Workbook Open Event Not Firing

查看:109
本文介绍了Excel VSTO,工作簿打开事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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键并运行代码时,显然事件处理程序不会执行.有任何想法吗?

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?

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

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

推荐答案

好,您知道Excel启动时不会调用 Open 事件,仅当您 open 现有工作簿.

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

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

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

我找到了一种解决方法,但是不得不说我只测试了1分钟,尝试一下让我们知道

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

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

这篇关于Excel VSTO,工作簿打开事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆