The Problem

A few days ago, I was toying with WiX (Windows Installer XML). When working with WiX, you need Guids for your product ID, upgrade ID, installer ID, …

To compile my installer, I needed valid Guids. So instead of always going to the web and use a guid generator, I decided to create a Macro in Visual Studio (2008) and link it with a shortcut to my keyboard.

Creating a Macro in Visual Studio

First open the Macro explorer. You can find it under “Tools > Macros > Macro Explorer” or by pressing “ALT + F8″.

Right click on MyMacros and choose “New Module”. Name it “GuidGenerator” and press the Add button.

You’ll notice that “GuidGenerator” has been added to MyMacros. Double click on the “GuidGenerator” file to open the editor.

Copy the following piece of VB code in your GuidGenerator Module.

     Sub CreateNewGuidWithUppercase()
        Dim textSelection As TextSelection = DTE.ActiveDocument.Selection
        textSelection.Text = Guid.NewGuid().ToString().ToUpper()
    End Sub

    Sub CreateNewGuidWithLowercase()
        Dim textSelection As TextSelection = DTE.ActiveDocument.Selection
        textSelection.Text = Guid.NewGuid().ToString().ToLower()
    End Sub

That’s it for your Macros. With these two methods, we can create an uppercased Guid or a lowercased Guid.

Linking the Macro with the keyboard in Visual Studio

Open the keyboard mappings dialog, you can find it under: “Tools > Options > Environment > Keyboard”.

In the “Show commands containing” textbox, start typing MyMacros. Our newly created Macros should appear a little something like “Macros.MyMacros.GuidGenerator.CreateNewGuidWithUppercase”.

Activate the “Press shortcut keys” textbox, by clicking in it. Then choose the shortcut you want to use, just by pressing the key-combination you prefer. I used “ALT + G, L” for the lower cased Guid and “ALT + G, U” for the uppercased Guid.

That’s all there is to it! You can test it by opening a file and pressing the shortcut. A fresh Guid will appear!