Tuesday, December 3, 2013

Visual Studio Time-Stamp

Earlier this year I wrote a post about how to insert a time stamp in Notepad++ (link).  I thought it might be helpful to share how I did the same thing in Visual Studio using a Macro.  I recently upgraded my hard drive, and had to stumble through setting everything up again, so I figured this was as good of place as any for a reminder.

It is a fairly simple process, the most annoying part is knowing where to look.  I don't have too much interest in knowing everything there is to know about Visual Studio, so I was thankful when I found it relatively straight-forward.  You may want to use this process in your research to build other macros.

A FEW NOTES:

  • You edit a Macro in an IDE for Macros, kind of like a sub-IDE inside Visual Studio
  •  You can test Macros on a file that is open in the main Visual Studio window, so you may want to have a file open for testing
  • Visual Studio comes with a bunch of sample macros, if you have them installed you should see them in the Macro Explorer. They may help you achieve what you are trying to achieve
  • I tested this in VS 2005, but the process should be similar in newer versions
 

THE PROCESS:

  1. Open "Macro Explorer"  Menu: View > Other Windows > Macro Explorer
  2. A small window (or side-bar) opens for Macro Explorer, expand the "MyMacros" and right-click and choose Edit
  3. This opens a the Visual Studio Macros IDE
  4. The default code to get you going is already there, so you should just need to create a Subroutine, here is the code I used:
    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports System.Diagnostics
    
    Public Module jmjMacros
        Sub InsertMod()
            Dim textSelection As EnvDTE.TextSelection
            textSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
            textSelection.Text = System.DateTime.Now.ToShortDateString() & " JMJ:"
        End Sub
    End Module
    
    (note that I have already renamed the "MyMacros" Module to "jmjMacros" in this example)

  5. Notice I used "jmjMacros" and appended a "JMJ:" at the end of the line, unless those are your initials you may want to change them
  6. You should be able to test your code. Make sure a file is open in the main Visual Studio window, and run the Macro to see if it inserts text.  If you don't have a file open, you may get an error message.  I had to switch back and forth between windows a couple times to confirm this was working
  7. That's it for the code!
The Renamed the Macro in Macro Explorer

Testing the Macro


KEYBOARD SHORTCUT:

If you want, you can map the macro to a keyboard shortcut:
  1. Under Visual Studio options (Tools > Options), choose Environment->Keyboard
  2. This just maps a keyboard shortcut to a chosen command:
    • I typed Ctrl+; into the "Press shortcut keys" field
    • Then I selected my Macro from the (ridiculously small) list of commands (see screenshot)
  3. Once I you have everything selected, click on the Assign button.  The command you set up should show under the "Shortcut currently used by" field
Setting the keyboard shortcut


If you are wondering why I use ctrl + ; (control key + semicolon key), that is the keyboard shortcut to insert the date in Google Docs.  I wanted to make sure I remembered it, so I made it the same for notepad++ and Visual Studio.

No comments:

Post a Comment