Helixoft Blog

Peter Macej - lead developer of VSdocman - talks about Visual Studio tips and automation

How To Create and Run a Macro in Visual Studio .NET

Welcome to my new blog. Here I will share some of my tips and tricks about VS that I learned or other people have asked for.

I start with the basic lesson about macros. Macros are still undiscovered area for many VS users though creating a macro is a quite simple task. Some people don't have time to play with macros, some don't know what they can use macros for and some even don't know that there are any macros in Visual Studio. This post is for all those people.

I will present several macros in the future so it is good idea to show how to put them in your Visual Studio and how to run them. Macro is nothing more than a function written in VB .NET and placed under your Macros. I can hear you asking: "Can I write macros using C# or some other language?". No, you cannot. Even when you are programming in C# or C++, your macros must be written in VB .NET. But don't worry, you don't need to learn VB .NET to try my macros. You will either record your macro in VS or copy and paste the code from here.

A macro can be created in two ways:

  1. Recorded in VS. This is mostly used when you want to create macro to automate repetitive tasks. These tasks mostly consists of steps that can be performed by using VS menus and dialogs. A macro only remembers all your actions. I will not talk about this kind of macros in the future. You can read more in "Recording Macros"
  2. Manually written. As previously mentioned, you can write your macro in VB .NET. We must use this method when we want to extend the functionality of VS instead of just recording the sequence of the actions. Macros can be really powerful and you can get desired effect without the need of writing an add-in.

To create a macro:

  • Open Macro Explorer. Go to Tools - Macros - Macro Explorer
  • Navigate to Macros - MyMacros. Here you can either use existing Module1 or create your own module by right-click and New Module...
  • Double click your module (e.g. Module1) to open Macros IDE.
  • Write your macro inside Public Module Module1. Module1 is replaced by your module name if you are using different module. As I wrote, macro is VB .NET function. You can try the following simple macro which shows a message window:
    Sub test()
        MsgBox("This is my macro")
    End Sub
  • Close Macros IDE.

To run the macro:

  • In Macro Explorer, double click test macro under your module (right-click and Run in VS 2005). This will run your macro which shows a message.
  • There are several other ways to run macro. You can for example assign a shortcut to it or create a button in menu or toolbar. I will show the latter in the next post. For more info see "Running Macros".


# asif 2006-05-22 09:16
hai good work
# Erion 2008-09-22 17:24
Thanks, it works really nice :)
# Nikhil 2008-11-14 15:27
Thanks, keep writing...
# whatispunk 2009-01-02 17:08
So far seems to work pretty good. You may want to consider adding \t* after the newline \n in the regex so it will find all matches regardless of the code indentation.
# jamila 2009-05-14 11:09
# Harish 2009-07-03 05:50
I want to check whether a word document is having macros in it using c#.net. Can you help me in this regard
# Nene 2009-08-11 11:57
This was really cool! ...didn't know about macros. well done!
Guess I'm gonna start abusing it.
# spencer 2009-11-05 14:02
Thank you, that is great. Sir, you are a gentleman and a scholar.
# Hoque Md.Nazmul 2010-05-28 07:49
Helped me for writing Macros in VS.
# Praveen 2010-08-05 14:27
i have already one macro written in excel VBA, wan to integrate this macro when some event is occured on form ...can we call macro from C# code

# dangerous max 2011-04-05 09:26
THis is Good Idea.
But lets change this idea into professional implementation.
NOw make macro which record the online form filling step by step e.g
as in page
first name
last name
# anil 2011-07-29 14:02
IM new to Macros in VS2008,
This helped me, Naming the Module & content with the same name.
It might not be big for every body, but, for me it helped
# pani 2011-08-05 09:10
i have already a macro in c# how to convert this in to method an uh explain it
# Mahasivan 2011-10-25 08:34
Thanks a lot, its really helfull
# ailton 2011-12-12 22:25
Dear Sir, I´m from Brasil. Your explanation is fantastic and easy to understand.
I have an Icon (launch visual studio 2008) into my software, but when I click the button says ( vs 2008 not instaled). I need to buy visual studio 2008 or it is a free software??? Can you help me????
Where I find visual studio 2008???
Another question: If I need complex macro you can help me like a job payed.
Let me say: My english is not very good, but I work online in some little structural projects with one indian and a north ireland man, thad I knew in a internet group...Languag e is not a problem...NUmbe rs are the same in all the world.
My presentation: I´m a little steel fabricator on Brasil with 5 employees.
If you want to know, you can see in www.youtube.com/user/acovertical
Thanks by your attention
# Peter 2012-01-06 12:05
Very useful explanation, thanks a lot.
# Khalid 2012-01-19 10:21
Just wanted to add that macros are not supported on the Express versions of Visual Studio. At least I don't see the option on my Visual C# 2010 Express.

Good article nevertheless.
# Paul 2012-03-29 15:10
# Dylan Parry 2012-08-02 10:23
@Khalid It seems that macros aren’t supported by VS 2012 at all. They’ve totally removed the feature as it turns out that less than 1% of users used macros so they decided not to implement it in the new version :(

Comments are now closed for this entry


Start generating your .NET documentation now!
Free, fully functional trial