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.
My VB.NET application supersedes the functionality of the open event. So if a user opens the workbook in Excel I want the magic to occur. 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.
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?
VB.NET Code looks like this:
' 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.