1
votes

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 ?

2

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
0
votes

Okay, I did it! I simply created a new function in an existing Macro

Private Sub MyMacro 
 MsgBox "HI" 
End Sub

And then, I called this function from ThisWorkbook

 Private Sub Workbook_Open()
 Run "MyMacro"
 Exit Sub