[英]Workbook_Open handler not firing in add-in when opening workbooks
I have an add-in for Excel that I attempting to use to insert code to automatically rename the workbook if certain criteria are met. 我有一个Excel加载项,我尝试使用它来插入代码,以便在满足某些条件时自动重命名工作簿。 When exporting from our repo, all workbooks have the same name which results in 'Excel cannot open two workbooks with the same name at the same time' error happening over and over again.
从我们的仓库导出时,所有工作簿都具有相同的名称,导致“Excel无法同时打开两个同名的工作簿”错误一次又一次地发生错误。 I've gotten the code to work when the user clicks a button in the add-in, but my attempts to do the same by placing the code in the 'ThisWorkbook' section of the addin does not seem to work.
当用户单击加载项中的按钮时,我已经获得了代码,但是我尝试通过将代码放在加载项的“ThisWorkbook”部分中来做同样的事情似乎不起作用。 Is there a way to do this via the add-in?
有没有办法通过加载项执行此操作?
Please find my code below. 请在下面找到我的代码。
Private Sub Workbook_Open()
Dim wbPath As Variant, wbNewname As Variant, newPath As Variant, wbname As Variant
Dim fileExtension As Variant
If ActiveWorkbook.Name Like "SR.*" Or ActiveWorkbook.Name Like "SR (#).*" And Application.ActiveWorkbook.Path Like "*Content.IE5*" Then
Excel.Application.DisplayAlerts = False
wbname = ActiveWorkbook.Name
fileExtension = Right(wbname, Len(wbname) - InStrRev(wbname, ".") + 1)
wbPath = Application.ActiveWorkbook.Path
wbNewname = "SR" & Int((465480 - 1 + 1) * Rnd + 838588)
newPath = wbPath & "\" & wbNewname & fileExtension
ActiveWorkbook.SaveAs Filename:=newPath
Excel.Application.DisplayAlerts = True
Set wbPath = Nothing
Set wbNewname = Nothing
Set newPath = Nothing
Set wbname = Nothing
Set fileExtension = Nothing
End If
End Sub
ThisWorkbook
is your add-in - its Open
event fires when the add-in file gets opened, not when any workbook is opened beyond that point. ThisWorkbook
是您的加载项 - 当加载项文件被打开时,它的Open
事件会触发,而不是在超出该点时打开任何工作簿。
What you want is to handle application-level events, more specifically the WorkbookOpen
event. 您想要的是处理应用程序级事件,更具体地说是
WorkbookOpen
事件。 Add a private WithEvents
field to your ThisWorkbook
module: 将一个私有的
WithEvents
字段添加到ThisWorkbook
模块:
Private WithEvents App As Excel.Application
In the Workbook_Open
handler (ie at startup), Set
that object reference: 在
Workbook_Open
处理程序中(即在启动时), Set
该对象引用:
Private Sub Workbook_Open()
Set App = Application
End Sub
Now, look near the top of the code pane - there are two dropdowns. 现在,看看代码窗格的顶部附近 - 有两个下拉菜单。 The left-hand dropdown is listing all available event providers and interfaces (ie
Workbook
since ThisWorkbook
is a Workbook
object, but also any WithEvents
fields and Implements
interfaces you've defined in that module); 左侧下拉列表列出了所有可用的事件提供程序和接口(即
Workbook
因为ThisWorkbook
是Workbook
对象,还有您在该模块中定义的任何WithEvents
字段和Implements
接口); select your App
field. 选择您的
App
领域。
Then, the right-hand dropdown will list all available events you can handle for App
. 然后,右侧下拉列表将列出您可以为
App
处理的所有可用事件。 Pick the WorkbookOpen
event, and the VBE will automatically create a procedure stub with the correct signature for you: 选择
WorkbookOpen
事件,VBE将自动为您创建一个具有正确签名的过程存根:
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
End Sub
In that handler procedure, you can put the code you want to run whenever a workbook is opened in Excel. 在该处理程序过程中,只要在Excel中打开工作簿,就可以放置要运行的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.