You could use an application event to look for opening an existing workbook or
creating a new workbook.
Create a new workbook.
hit alt-f11
find your workbook/project and rightclick on it.
Insert|class module
It should be called Class1 (default name).
Paste this in that code window:
Option Explicit
Public WithEvents xlApp As Excel.Application
Private Sub xlApp_NewWorkbook(ByVal Wb As Workbook)
MsgBox "New workbook"
End Sub
Private Sub xlApp_WorkbookOpen(ByVal Wb As Workbook)
MsgBox "Opening an existing workbook."
End Sub
Now find your "thisworkbook" module under the Microsoft Objects category in your
project.
rightclick on it and select view code.
Paste this into that code window:
Option Explicit
Public WithEvents xlApp As Excel.Application
Private Sub xlApp_NewWorkbook(ByVal Wb As Workbook)
MsgBox "New workbook"
End Sub
Private Sub xlApp_WorkbookOpen(ByVal Wb As Workbook)
MsgBox "Opening an existing workbook."
End Sub
Save this as addin (file|saveas and scroll down the save as type list).
remember the location where you saved it.
Then close excel.
reopen excel and choose
tools|addins
Select your addin--you may have to browse for it.
now try it out--open a workbook, start a new workbook.
(You'll replace the msgboxes with meaningful code, though.)
If you want to read more about application events, visit Chip Pearson's site:
http://www.cpearson.com/excel/AppEvent.htm
Chip also has some notes about events at:
http://www.cpearson.com/excel/events.htm
If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
David McRitchie also has notes at:
http://www.mvps.org/dmcritchie/excel/event.htm