[英]Workbook_Open event in Excel Add ins works only for first file opening
Workbook_Open event in Excel Add-ins works only for the first file but If I open next files keeping the first file is open it is not functioning, Can anyone please help me to resolve this issue? Excel 中的 Workbook_Open 事件加载项仅适用于第一个文件,但如果我打开下一个文件保持第一个文件处于打开状态,则它不起作用,谁能帮我解决这个问题?
I have used this below code in Thisworkbook module to create Add-in and selected Add-In name in options also我在 Thisworkbook 模块中使用了以下代码来创建加载项并在选项中选择了加载项名称
Code:代码:
Sub Workbook_Open()
MsgBox "Welcome..!!"
End Sub
Any help is appreciated..!!任何帮助表示赞赏..!! Thanks谢谢
It doesn't work for the first file opening.它不适用于第一个文件打开。
ThisWorkbook
represents the document that's hosting the VBA code - in this case, the add-in "workbook": this message box will pop when the add-in is opened, when Excel starts up. ThisWorkbook
表示托管 VBA 代码的文档 - 在这种情况下,加载项“工作簿”:当 Excel 启动时,加载项打开时会弹出此消息框。
If you want to run code whenever any workbook is opened, you need to handle events at the Application
level.如果要在打开任何工作簿时运行代码,则需要在Application
级别处理事件。
Declare a WithEvents
variable in your ThisWorkbook
module:在ThisWorkbook
模块中声明一个WithEvents
变量:
Option Explicit
Private WithEvents AppEvents As Excel.Application
Assign that object reference on open:在打开时分配 object 参考:
Private Sub Workbook_Open()
Set AppEvents = Me.Application
End Sub
Now select AppEvents
from the left-side dropdown at the top of the code pane, and pick the WorkbookOpen
event on the right-side dropdown - that will create a handler that looks like this:现在 select AppEvents
从代码窗格顶部的左侧下拉列表中选择,然后在右侧下拉列表中选择WorkbookOpen
事件 - 这将创建一个如下所示的处理程序:
Private Sub AppEvents_WorkbookOpen(Wb As Workbook)
End Sub
If you put MsgBox Wb.FullName
here, you should get a message box with the newly opened workbook's path/filename every time a workbook is opened.如果您将MsgBox Wb.FullName
这里,您应该在每次打开工作簿时收到一个带有新打开的工作簿路径/文件名的消息框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.