Execute macro on event

  • Thread starter Thread starter Bob Altman
  • Start date Start date
B

Bob Altman

Hi all,

In VS 2005, I want to have a macro run whenever I save a document. (I want to
update my assembly version when I modify a source file in a project.)

I've spent a couple of hours digging through the docs for creating macros, but I
can't decipher them well enough to get this working. Specifically, I can't
figure out how to actually get the macro to trigger when I save a document.
I've created and initialized what appear to be the appropriate event handlers in
my macro, but nothing interesting happens when I save a document. I'm hoping
that someone can post complete step-by-step instructions to do something simple
like display "Hello World" when a document is saved, or provide a link to a
decent writeup on this subject.

TIA - Bob
 
This can be done easily. Use DocumentEvents.DocumentSaved event. See my
article how to use IDE events with macros,
http://www.helixoft.com/blog/archives/32

In short:
1. Go to menu Tools - Macros - Macros IDE...
2. In the Macros IDE Class View navigate to MyMacros - {} MyMacros -
EnvironmentEvents. Open (double-click) EnvironmentEvents.
3. Add handler for DocumentEvents.DocumentSaved and place your code
there. For example the following handler shows the file name of
currently saved document:
Private Sub DocumentEvents_DocumentSaved(ByVal Document As
EnvDTE.Document) Handles DocumentEvents.DocumentSaved
MsgBox(Document.FullName)
End Sub
 
Hello Bob,

It seems you are looking for a Saved Event which fires when end user saved
any file. If I misunderstood anything here, please don't hesistate to
correct me.

Have you tried with DocumentEvents.DocumentSaved? If you are not very
fimarly with VS Macro, you may follow the steps as below:

1) In Your Vsisual Sutdio IDE, Click Tools|Marcos|Marcos IDE. Then,
Microsoft Visual Studio Macros IDE will show up.
2) In Microsoft Visual Studio Marcors IDE, Dould click
"MyMacros"|"EnvironmentEvents" file.
3) Select "DocumentEvents" on the "General" list box in navigatio bar.
4) The, select "DocumentSaved" on the right listbox.
5) VS Marcor IDE addes the following code into EnviormentEvents file
automaticlly.

Private Sub DocumentEvents_DocumentSaved(ByVal Document As EnvDTE.Document)
Handles DocumentEvents.DocumentSaved
End Sub

Then, you add any task you want to do when end user saved file. For
example: the following code snippet will show a messagebox shows which file
you saved in Visual Studio.

Private Sub DocumentEvents_DocumentSaved(ByVal Document As EnvDTE.Document)
Handles DocumentEvents.DocumentSaved
System.Windows.Forms.MessageBox.Show("Save File Name:" &
Document.Name)
End Sub

Hope this helps, Please feel free let me know if there is anything unclear.
It's my pleasure to assist you.

Have a great day,
Best regards,
Wen Yuan
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks Peter and Wen Yuan! In typical fashion, I was making things much
harder than they need to be. The first thing I did when I got to the Macro
IDE was to create my own macro (I renamed the default Module1 macro to
UpdateVersionNumber, since that's descriptive of the task I want to
perform), then I copied the "Automatically generated code, do not modify"
section to my custom macro. (At that point I was wondering what was magic
about the "automatically generated" code, since it was just declaring module
level variable with support for wiring up event handlers.) When I wired up
the event handling code *in my custom macro*, nothing happened. It didn't
occur to me (and none of the documentation I dug out hinted at the fact)
that the EnvironmentEvents macro is magic. I guess it's time for a trip to
Microsoft Connect to suggest a documentation improvement...

To make matters worse, the docs for EnvironmentEvents
(http://msdn2.microsoft.com/en-us/library/0b27f9kz(VS.80).aspx) make a big
point of instructing me to add code to the OnMacrosRuntimeReset and
OnStartupComplete event handlers. They neglect to mention the small item
that I can use the "built in" events by simply implementing an event handler
for any of the events in the "Automatically generated code" section. I
think that what they're trying (unsuccessfully) to tell me is that I need to
add all of that initialization claptrap *only* if I add an event handler
that's not already in the "Automatically generated code" section.

Thanks again!

Bob
 
Hello Bob,
Thanks for your reply.

You are welcome. It's my pleasure to assist you. It seems the MSDN Document
is misleading. And you have done much harder work than what you need to do.
We may support MSDN website to add more detailed information about how we
can use the build-in EnvironmentEvent. Thereby, end users won't confuse. I
will log it into our internal DB for Improvement. Thanks for your posting.

Anyway, we are glad to assist. If you face any further issue or there is
anything unclear in Marco, just feel free to let us know. We are standing
by.

Have a great day,
best regards,
Wen Yuan

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top