Automating Tasks in Visual Studio

By | October 26, 2010

Ever wondered how to automate tasks in Visual Studio such as auto fill forms, paste into multiple locations or even print all open documents? Well with the use of VS Marcros it will be possible and the possibilities are endless only restricted by your imagination.  VS Macros is a very powerful tool within Visual Studio and most probaly most developers don’t use this or whats worse they might have bought a third party solution to automate some tasks in Visual Studio.

Having said that this article is to show you how to develop a simple macro to get you started.  This macro that I will run through is how to make a shortcut for commenting within Visual Studio Text Editor, that comment will include the current time the comment was made and username who commented it, which is useful specially if you are working with other developers as well.

So lets get started.

1. Open the Macro Explorer

First you have to go to macro explorer and check whether the macro you want to make is already available as there are some built in macros that come with Visual Studio.  You can do that by going to Tools -> Macros -> Macro Explorer.  On the Macro Explorer view you can see that there is a  Sample Project, browse through that and check whether the solution you want to develop is avaibale if not proceed to step 2

2. Create a New Macro Project

If the one you need is not on the Sample Macros then start developing your own.  You can create your own project, use the MyMacros that is already in there or even the Samples, its all up to you.  For this sample we use “MyMacros”.

3. Create a New Module

Now right click on the “MyMacros” project and choose “New module”

and give it a useful name.   For this sample I will use “MyCustomModule”.

4. Create a New Macro

Now right click on that new module you just created and select “New macro”, you will now be presented with the Marco Editor.  Now you have a new Sub which is “Macro1”, rename it to what you want, in this sample I will use “BetterComments”.

5. Start Developing

The one we want to achieve here is to create a better comment, use the code below as a sample.

If you notice there is a function there called LineOrientedCommentStart, this was just copied in the sample macros and it adds that comment character in front of the text comment, it also checks for the document extension to place the proper comment charaters, you can extend this to use other file types as well.

Importsss System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module MyCustomModule
    Sub BetterComments()
        Dim textSelection As EnvDTE.TextSelection

        textSelection = DTE.ActiveWindow.Selection

        textSelection.Insert(" ---------------------------------------------------")

        textSelection.Insert(" " + Date.Now + " - " + System.Environment.UserDomainName + "" + System.Environment.UserName())

        textSelection.Insert(" ")

        textSelection.Insert(" ---------------------------------------------------")
    End Sub

    Function LineOrientedCommentStart(Optional ByVal document As Document = NothingAs String
        Dim extension As String

        If (document Is NothingThen
            document = DTE.ActiveDocument
        End If

        extension = document.Name
        If (extension.EndsWith(".cs"Or extension.EndsWith(".cpp"Or extension.EndsWith(".h"Or extension.EndsWith(".idl"Or extension.EndsWith(".jsl")) Then
            Return "//"
        ElseIf (extension.EndsWith(".vb")) Then
            Return "'"
            Throw New Exception("Unrecognized file type. You can add this file type by modifying the function Utilities.LineOrientedCommentStart to include the extension of this file.")
        End If
    End Function
End Module

6. Assign a Keyboard Shortcut

Now youre done in developing you macro, save it and assign a keyboard shortcut.  Go to Tools -> Options and the dialog below will show.  Type the name of your macro in the “Show commands containing” text box to view you newly developed macro and assign shortcut keys using “Text Editor” only (you dont want to activate that macro in Class Diagram or UML Designer).  In this sample we use Alt + Insert.

7. Use your Macro

Now on text editor press Alt + Insert and you should see your new comment now.


Leave a Reply

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