[英]How to prevent an Excel Workbook_Open event from firing when opened by VB.NET
I have a workbook that does some whizbang stuff when you open it. 我有一本工作簿,当您打开它时会做一些奇怪的事情。 That's great and I want to keep it the way it is but I also want to share some of the internal functions with a VB.NET project that relates to this magic workbook. 太好了,我希望保持原样,但我也想与与此魔术工作簿相关的VB.NET项目共享一些内部功能。
My VB.NET application supersedes the functionality of the open event. 我的VB.NET应用程序取代了打开事件的功能。 So if a user opens the workbook in Excel I want the magic to occur. 因此,如果用户在Excel中打开工作簿,则希望发生这种情况。 If I open that same workbook from my VB.NET application I want to suppress the Workbook_Open event so I can do the magic from VB.NET instead. 如果我从VB.NET应用程序中打开相同的工作簿,则要取消Workbook_Open事件,以便可以从VB.NET进行操作。
Is there a flag I can set before I call Open that will allow me to run macros from that workbook while also suppressing the open event? 在调用Open之前,是否可以设置一个标志,使我可以从该工作簿运行宏,同时还可以抑制open事件?
VB.NET Code looks like this: VB.NET代码如下所示:
' Set up Excel
Dim excelApp As Application
Dim thisWorkbook As Workbook
Dim excelBooks As Workbooks
' Start Excel
excelApp = New Application With
{
.Visible = False ' Ninja mode
}
' Open the workbook
excelBooks = excelApp.Workbooks
thisWorkbook = excelBooks.Open(My.Settings.ExcelFileName)
' Run the subroutine.
excelApp.Run("WhizBang", SecretVBNETMagicPassedInToExcel)
' Close the workbook and release the COM objects.
thisWorkbook.Close(SaveChanges:=False)
Marshal.ReleaseComObject(thisWorkbook)
Marshal.ReleaseComObject(excelBooks)
excelApp.Quit()
Marshal.ReleaseComObject(excelApp)
GC.Collect()
由于您不想运行的代码位于Workbook_Open事件处理程序中,因此可以在打开工作簿之前为应用程序禁用事件,以防止调用处理程序代码。
excelApp.EnableEvents = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.