Reputation: 1277
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
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?
Edit: The event handler will run if I open a workbook from the workbook that opens, but will not run for the original workbook itself.
Upvotes: 1
Views: 2473
Reputation: 688
Well, you know the Open event is not called when Excel starts, the event is called only when you open an existing workbook.
There is an event **NewWorkbook* which interestingly enough is not fired either ...
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
Upvotes: 1