Run macro when workbook opens - from Addin

Formula11

Active Member
Joined
Mar 1, 2005
Messages
475
Office Version
  1. 365
Platform
  1. Windows
I'm using an addin for a macro, and need to test for the workbook name.
The code below works but the macro "initial_do" runs twice.

Tried to call up from Workbook_Open but no luck.
One of the difficulties seems to be that getting workbook name seems to be triggered after Workbook_Open.


ThisWorkbook
VBA Code:
Option Explicit

Private WithEvents App As Application '... for new Workbooks???

Private Sub Workbook_Open()
    Set App = Application
End Sub

'To handle workbooks opened after the first instance???
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    Application.Run "initial_stuff"
End Sub

Module1
VBA Code:
Option Explicit

Public RunWhen As Double
Public Const RunIntervalSeconds = 0
Public Const RunWhatInitial = "initial_do"

Private Sub initial_stuff()
    RunWhen = Now + TimeSerial(0, 0, RunIntervalSeconds)
    Application.OnTime EarliestTime:=RunWhen, Procedure:=RunWhatInitial, Schedule:=True
End Sub

Private Sub initial_do()
    MsgBox ActiveWorkbook.FullName
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I presume you mean it runs 2x in succession. I copied your code and msgbox was raised only once for each wb I opened. Perhaps your code needs to be compiled?
 
Upvote 0
Thanks Micron, did you save code as add-in and automatically run it with Excel, this is the instance when it runs twice, yes in succession.
If called up normally (not from addin), then only runs once.

This is the first time I've heard of compiling code, assume you mean error checking by this. Clicked Debug, Compile VBAProject, nothing happened.

Doesn't make sense to be called up twice from the active workbook (using ActiveWorkbook.FullName).
If called up once each from addin, and once active workbook this would have made sense.
 
Upvote 0
Doesn't the following work for you as expected?
VBA Code:
Option Explicit

Private WithEvents App As Application '... for new Workbooks???

Private Sub Workbook_Open()
    Set App = Application
End Sub

'To handle workbooks opened after the first instance???
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    If Not Wb.IsAddin Then MsgBox Wb.FullName
End Sub
 
Upvote 0
Solution
No I missed that fact. The behaviour makes sense to me if the add in is regarded as an application. When it loads, it would trigger the code, no?
Anyway, no need to reply since you have a nice solution. I learn something here almost every day.
 
Upvote 0

Forum statistics

Threads
1,225,297
Messages
6,184,133
Members
453,215
Latest member
pschatzow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top