I opened the MyMacros project in the Macros IDE, opened the EnvironmentEvents module and looked at the code.
The line...
<System.ContextStaticAttribute()> Public WithEvents DocumentEvents As EnvDTE.DocumentEvents
...was not present so I added it just after the Module declaration as I had seen in the Sample macros.
Next I used the dropdowns at the top to select the DocumentOpened event and just put the code...
MsgBox("test")
... into the sub.
Saved, closed the macros IDE then tried opening documents in Visual Studio. Nothing happens - the documents just open as usual and no message box is seen. Putting a breakpoint on the line in the macros IDE it never gets hit.
Also even if I pick out the WindowEvents.WindowActivated or TaskListEvents.TaskAdded neither of them fire either.
How do I get Visual Studio to start firing this macro code please

DocumentEvents.DocumentOpened
jan.zajic
All helpful, thanks very much.
For the main part it was important to check that I wasn't doing anything obviously wrong.
I think I've got it working now. Crashes in the macro IDE made me think that it might be the location of the macro projects that was one of the issues.
One of the issues seemed to be related to the macros being on my shared profile network drive. I changed My Documents to point to a directory on the C: drive and moved all the files there. Also I added a couple of new macro projects, put the code in one of them and set the other one as the "recording project" then did a combination of loading/unloading the macro projects and restarting the IDE and eventually it started to work.
I also made sure the Run Macros setting in the IDE options was ticked as you suggested (it was already set).
I've got another question but I'll do a different post for that to make it easy for people searching in future!
PeterPan1120
Hi,
First, check the following things in the main IDE:
- In the Tools, Options menu, Add-Ins/Macros security section, check the checkbox "Allow macros to run"
- In the Macro Explorer, select the macro project with the events and ensure that the Security property is set to "Enable Event Handling Code"
Now, in the macros IDE:
- Create your event handler and put a message box
- Create a second macro with this code:
Sub f1()MsgBox(
"test") End SubPut the cursor in the line MsgBox "Test" of f1 and click F5 to run the macro, which should work. This guarantees that 1) Your macros work 2) The module does not have compilation errors. If you have a module with compilation errors no macro can run and the event handlers don't work.
Now Save, close the macros IDE and test your macro event handlers.
All that said, I have seen the problem that you describe but I don't know what causes it. At the one hand, I have tested that breakpoints do not seem to work in macro event handlers. At the other hand, when you have this declaration:
<System.ContextStaticAttribute()>
Public WithEvents DocumentEvents As EnvDTE.DocumentEventsSomeone has to initialize the DocumentEvents variable to DTE.Events.DocumentEvents (otherwise it would be Nothing). I think that the macros IDE does it behind the scenes without visible code but when does it do it I think that when the macros IDE is initialized or reset. I have found documentation about the OnMacrosRuntimeReset event here:
DTEEventsClass.OnMacrosRuntimeReset Event
http://msdn2.microsoft.com/en-gb/library/envdte.dteeventsclass.onmacrosruntimereset(VS.80).aspx
and also this problem:
The OnMacrosRuntimeReset event does not fire when you load a macro in Visual Studio 2005 or in Visual Studio .NET
http://support.microsoft.com/kb/833897
So you may want to put a message box also in that event to see when the variables are initialized.