This question appears from time to time in forums, so I wrote an article about it:
HOWTO: Initializing new events in Visual Studio macros
http://www.mztools.com/articles/2007/MZ019.htm
When it was already written I discovered that a very similar one appears in the MSDN docs:
How to: Create an Event Handler in a Macro for a Specific Type of Project
http://msdn2.microsoft.com/en-us/library/ms228948(VS.80).aspx