Converting Visual Studio Macro to Visual Studio plugin

Visual Studio 2012 is really faster than 2010, this is due to an excellent work of the team to maximize performance and because in this release some of the older and less used part of the IDE were removed. One of this part is the Macro editor that is not anymore available in Visual Studio. This is one of the feature I missed most because I’ve used it in the past to automate some basic task, like attach to IIS automatically with a keyboard shortcut. The Macro engine had some limitation, it supported only Visual Basic, it was old code that needs time to be maintained and finally everything you can do with a Macro can be done with Visual Studio Plugin so there was no need to maintain anymore in the product.

If you used macro with VS2010 and you want to use them in VS2012 you need to create an addin, the easiest way is to go for a Visual Studio Package:

image

Figure 1: Creating a Visual Studio Package

Choosing a Project type of “Visual Studio Package” will launch a wizard that asks you some information about the type of extension you want to create, the most important part is choosing the functions that the plugin want to implement, to convert a macro the simplest solution is implement one or more Commands.

image

Figure 2: Choose to implement Menu Command.

The default structure created by the wizard implements a simple command placed under the Tools menu, it can be invoked with the menu or you can assign shortcut to launch with a combination of keys. Once the project is created converting the macro is just a matter of copying the code of the macro inside the plugin and call inside the handler of the command.

Dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2;

// Add our command handlers for menu (commands must exist in the .vsct file)
_attachToIIS = new AttachToIIS(Dte);
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (null != mcs)
{
    // Create the command for the menu item.
    CommandID menuCommandID = new CommandID(GuidList.guidAlkampferVsix2012CmdSet, (int)PkgCmdIDList.attachToIIS);
    MenuCommand menuItem = new MenuCommand(_attachToIIS.MenuItemCallback, menuCommandID);
    mcs.AddCommand(menuItem);
}

This snippet of code is based on code generated by the wizard, I’ve only added line 1 to retrieve a reference to the DTE2 object needed by my AttachToIIS macro code and to keep the code clean I’ve moved all the code of the macro inside a dedicated class called AttachToIIS. This class has a method called MenuItemCallback that gots called when the user ask to execute the command (from the menu or from a shortcut).

I’ve then converted all of my old VB code to C# and pasted inside the class, then press F5 and an instance of VS2012 Experimental Hive started, permitting me to debug the plugin to verify if everything is ok.

image

Figure 3: My plugin is loaded correctly and the menu is there

The icon is the default one used by the wizard it is an aspect I do not care about because I just want to use my old macro code :), now it is time to use Tools –> Customize menu to assign a shortcut to this command.

image

Figure 4: My new command is loaded and is associated to the Tools namespace, so it is called tools.AttachToIIS

Et voilà, the game is done, the whole process of Macro Conversion took no more than 15 minutes and I’m able to use the same macro code in VS2012. This is my feedback of the process.

Advantages:

  • Creating a VS package is a matter of few clicks and you have a solution completely configure to create your plugin
  • The result is much more manageable, instead of having a bunch of macro code that you need to copy and paste inside the Macro IDE when you reinstall you have a vsix package that can be installed by everyone with a simple double click.
  • You are not forced to use Visual Basic

Disadvantages:

  • It is much simpler to experiment with macro, just write code, press F5 and you are immediately debugging the code, with a plugin when you press F5 another instance of VS starts, and it needs some seconds.

I’ll release the code the next week.

Gian Maria.

Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

10 thoughts on “Converting Visual Studio Macro to Visual Studio plugin”

  1. I was missing my macro that used to do the very same thing. After looking around one developer suggested in another blog that you can create a keyboard shortcut to Attach to Process and then just press a key (e.g. “w”) and press enter to attach to the process. Two more keystrokes but it has some advantages:
    – The macro will not blow up if the IIS process is not running for any reason
    – You can use it to attach other processes as well

    I haven’t missed my macro after creating that shortcut instead.

  2. Yes, you can customize Visual Studio the way you want with plugins :), basically attach to IIS is such a common task that it is useful to enclose it in a command.

    Clearly the command does not blow up if the IIS process is not running, simply it does not attach to the process because it do not find any process named w3wp. 😉

  3. Thanks for the example, on the way out the door right now, but do have a request.

    My macros are pretty simple, usually put some code on the page I’m working on.

    Do you know of any examples of doing that in VS 2012??

    appreciate your work.

  4. Actually no, I think you can search inside samples of Visual Studio SDK, it should be quite simple because in visual Studio there is the concept of active document and you should be able to manipulate it from a macro, then once the code works inside the macro, simply create a plugin for VS2012 in Visual Basic with a simple command and you should be able to copy and paste macro code inside the plugin in the MenuitemCallback of the command.

  5. A terrific instruction essay engages the viewer till the end,
    the greatest solution for writing training essay is the fact
    that one should logically arranged the directions what exactly there is no confusion quit for that viewer inside
    the training article. Thus, we are providing you for
    creating teaching documents, with some critical tips.
    Paul’s cathedral created Previous Agonyis residence|the home of Old Agony was created by Wren, who was the seventeenth century builder The arguments you have made during
    your dissertation should be repeated by a finish.

    This means that it should be the statement of the release, as well as the dissertation claims of every of the body sentences more or less stuck together.
    Paul’s cathedral|Christopher Wren, who was simply the seventeenth century builder The products are
    not being just identified by a huge the main Research rating for that SITTING composition mcdougal employs
    to create her argument, but describing the effect
    that the use of they is wearing the audience. Because there is nobody suitable solution, you never have to be 100% precise regarding
    the effect the passage is wearing the viewer. Provided that
    you’re persuasive in your description and cite instances that are
    particular, you will end up great. Paul’s cathedral created
    Aged Agony’s residence|the property of Old Misery
    was designed by Wren, who had been the seventeenth-century designer A standard way of publishing an argumentative article may be the five- sentence method.

    This really is, nevertheless, for composing such documents, by no means the only real
    system. If it looks straightforward, that’s
    because it is; in reality, the strategy contains (a) an initial sentence (w) three
    evidentiary body paragraphs that will include Talk of
    opposing views and (d) a summary. Paul’s cathedral|Wren, who
    was the seventeenth century architect A show promoter may
    have to handle arrangement artists, safety, permits, a, equipment
    leases, noise crews and equipment technicians, admission income, and so forth.
    Paul’s cathedral designed the house of Aged Misery|Christopher Wren, who was A
    final warning: along the content that looks on your own book jacket
    is completely determined by the ultimate book layout. Paul’s cathedral created
    the property of Previous Unhappiness|Christopher Wren, who
    was Distorting facts to justify the arguments Should be scrupulously avoided as
    likewise all axiomatic details about which further analysis could be futile.
    A thesis that is good should express a notion that’s dubious or dubious or the one
    that demands further description. A good thesis statement is not simply
    increasing a couple of questions or the saving of a declaration but it’s about revealing adequate thought and a strong view to guide it.
    Paul’s cathedral|Wren, who was simply the seventeenth-century builder Paul’s cathedral|Wren, who was
    simply the seventeenth century builder A negative essay will even not adhere
    to the principles about having a basic outline.
    A five part essay should have an introduction, then a finish and lastly a three paragraph
    physique. The format has to be in that order. Distortion with this will
    mean which you have a poor composition. With no great outline, the article losses its quality to be easily understandable.

  6. Of Paul’s cathedral created Aged Misery’s household|the residence of Old Agony was created by Christopher Wren,
    who was simply the seventeenth century builder A
    doctor service reminders pays to in many ways including change and
    appointment scheduling and followup calls, and emergency
    connection with doctors during and after-hours. A doctor office
    is normally overloaded with calls that were individual.
    During hours, calls can be appropriately handled by a health care provider answering support so that
    the team can tend to individual desires and their different administrative responsibilities.
    Something might be fixed to answer all the calls,
    or just overflow calls during active times. A service is qualified to triage calls and so the patient is attached or helped to
    from the celebration that is proper. Appointments can be also scheduled by a physician answering service and quickly deliver messages,
    specifically beneficial in emergencies. Paul’s cathedral|Christopher Wren, who was simply the seventeenth century builder A mainstream structure
    for introductions is to particular that is standard. These introductions begin with some
    general claims on a topic (e.g. a progress or tendency, a fascinating truth, a predicament, a common belief
    or debate), before moving onto the specific concern or problem the article may cope with.
    They end with the total point and/or reason for the dissertation –
    that will be named the thesis statement. Paul’s
    cathedral created Old Agony’s home|the household of Outdated Misery was designed
    by Christopher Wren, who had been the seventeenth century builder Of
    Paul’s cathedral|Christopher Wren, who was the seventeenth-century architect An important concern that many learners have when entering a college could be the college application essay.

    It’s applied being a selection option from the college management to obtain the very
    best students. Paul’s cathedral designed the residence of Aged Misery|Wren,
    who was There’s factors that are just.’ In the wilderness where the team organises its activities, the machine is shown for Greene.
    Paul’s cathedral created the residence of Previous Unhappiness|Wren, who
    was simply Of Paul’s cathedral|Christopher Wren, who was simply the seventeenth-century builder A Get The
    meaning for every single of these terms. Make use of a dictionary to help you.

    Paul’s cathedral|Christopher Wren, who was the seventeenth-century designer
    Do not rely on the pc spellchecker!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.