Point Deep

Mundeep's Tech Blog

Quickly Generating GUIDs in Visual Studio 2008

Posted by mundeep on March 27, 2008

One of the common tasks involved when creating sharepoint solutions is the generation of GUIDs. Visual Studio comes with a tool called GuidGen that lets you create GUIDs however it is annoying having to leave the Visual Studio environment.

I believe Visual Studio 2005 used to have it as an option under the Tools menu however i haven’t found where i can add the same shortcut in Visual Studio Team System 2008 (though i do notice that Visual Studio 2008 Professional DOES have a “Create GUID” shortcut under Tools – i’d still prefer the macro shortcut for ease of use especially when creating a lot of features).

I have however found a nifty alternate solution by Leon Zandman that describes creating a Macro to insert a guid into your current file. I’d just like to clarify some of the steps for those that haven’t dealt with Macros in Visual Studio 2008 before.

  1. Load Visual Studio 2008 and goto Tools -> Macros -> Macro Explorer (Alt-F8 for short)Macro Explorer
  2. Right-click on “Macros” then select New Macro Project
  3. Name your project (eg. GUIDGenerator) and choose a location to save it (note no space allowed in Project Name).
  4. This should give you a new project with a “Module1” sitting underneath it. Right-click on “Module1” and select “Rename” to give it a more meaningful name (eg. GUIDGenModule).
  5. Double-click on the newly renamed module and you should be shown the Visual Studio Macro IDE.
  6. Enter the following code (the “D” in ToString can be customised see Leon’s article):
    •    Sub Create_GUID()
              DTE.ActiveDocument.Selection.Text = System.Guid.NewGuid().ToString("D").ToUpper()
          End Sub
  7. Save and close the Macro IDE.
  8. Back in the main Visual Studio window goto Tools -> Options
  9. Goto the “Keyboard” option under the “Environment” tab.
  10. In the “Show Commands Containing” text box type in “Create_GUID”
  11. Select the Macro sub that you just created from the list (it should be the only one)
  12. Click inside the “Press Shortcut Keys” textbox then press your desired keyboard shortcut for inserting a GUID (eg. Alt+G as Leon suggested makes a lot of sense).
  13. Ensure the “Use Shortcut in” option is “Global” and click on “Assign”
  14. Close the options window and you should be able to start using your keyboard shortcut to quickly insert GUIDs into text!
  15. If you have any other Visual Studio windows open at the time you will need to close them and reload for the macro for the macro to be loaded (or you can goto the Macro Explorer window and manually load your Macro project)
Advertisements

13 Responses to “Quickly Generating GUIDs in Visual Studio 2008”

  1. Pablo said

    Tools…External Tools…Add …something like this in command C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\guidgen.exe

  2. Very good post, I use it a lot

  3. Brian said

    Very cool, this is great.

  4. Thank you! works great!

  5. Darren said

    Great post – something I have needed for a while. It does not work with all windows though (I need it for the SQLCE table editing window), so I might try to tweak it to work with other windows.

  6. Petar Repac said

    Tnx.

  7. James said

    As an old Delphi user doing GUID-intensive work in Visual Studio, this presented the perfect solution (AKA “It works like what I was accustomed to”). Again I perform one keystroke, and a GUID appears again.

    Thanks for taking the time to lay out the individual steps.

  8. […] article Quickly Generating GUIDs in Visual Studio 2008 has the full step-by-step rundown. It has a link to Generating GUIDs in the Visual Studio IDE, […]

  9. Try the website http://guidgen.com/
    I find it very handy.

  10. Adilson Carvalho said

    Great post.

    That Guid Generator that is shipped with Visual Studio is not pratical at all.

    If you deal with COM objects, you’ll need to generate a lot of GUID’s and this approach you explained is far the fastest way to get the job done.

    I’ve implemented it bound to Ctrl-Shift-G like I was used on my old times as a Delphi developer.

  11. Prasath C said

    excellent

  12. Anonymous said

    fantastic! I’m always copy and pasting GUID’s into my code

    thanks very much!

  13. Anonymous said

    Hats off 2 u…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: