I am doing some work on an excel addIn abc.xlam. This addIn is enabled in excel addIns. I want to launch an .exe file whenever I open up a worksheet (new or existing) in excel. I want to code this .exe launch part in the .xlam addIn at an event when a workbook is opened. Please tell me how can i do it ?
1
votes
2 Answers
1
votes
You need to access the NewWorkbook
event of the Excel appliation, so you need to set a reference to the Application
object when your addin loads.
Put the following example code in the ThisWorkbook
module:
Option Explicit '***** Always use Option Explicit!
Private WithEvents oXl As Application
Private Sub oXl_NewWorkbook(ByVal Wb As Workbook)
'***** Trapping the NewWorkbook event
Call MsgBox("It's me again. (" & oXl.Workbooks.Count & ")", vbInformation, "Hi. Again.")
'***** Your code here!
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'***** Remove reference to oXL object
Set oXl = Nothing
End Sub
Private Sub Workbook_Open()
'***** Set reference to the current Excel application
Set oXl = ThisWorkbook.Application
'***** Testing the oXL object
Call MsgBox("Hello, there! (" & oXl.Workbooks.Count & ")", vbInformation, "Hi")
End Sub