How To Create and Run 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.
Macro can be created in two ways:
- 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"
- 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.
- 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 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".

May 22nd, 2006 at 9:16 am
hai good work
September 22nd, 2008 at 5:24 pm
Thanks, it works really nice
November 14th, 2008 at 4:27 pm
Thanks, keep writing…
January 2nd, 2009 at 6:08 pm
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.
May 14th, 2009 at 11:09 am
LE COUDE DE CREATION D’UN MACRO ET LES SIGNET DANS WORD ET VB NET
July 3rd, 2009 at 5:50 am
I want to check whether a word document is having macros in it using c#.net. Can you help me in this regard
August 11th, 2009 at 11:57 am
This was really cool! …didn’t know about macros. well done!
Guess I’m gonna start abusing it.
November 5th, 2009 at 3:02 pm
Thank you, that is great. Sir, you are a gentleman and a scholar.
May 28th, 2010 at 7:49 am
Helped me for writing Macros in VS.
August 5th, 2010 at 2:27 pm
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
Regards,
praveen
April 5th, 2011 at 9:26 am
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
etc.etc
silent-storm@live.com
July 29th, 2011 at 2:02 pm
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
August 5th, 2011 at 9:10 am
helloz
i have already a macro in c# how to convert this in to method an uh explain it
October 25th, 2011 at 8:34 am
Thanks a lot, its really helfull
December 12th, 2011 at 11:25 pm
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…Language is not a problem…NUmbers 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
January 6th, 2012 at 1:05 pm
Very useful explanation, thanks a lot.
January 19th, 2012 at 11:21 am
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.