InsertGuid Visual Studio Macro

If you ever work on a project or installer that requires you to create some GUIDs frequently, you should make your life easier by creating a Visual Studio macro that will insert a GUID wherever your cursor is. Here’s how to do it:

Create the Macro

  • Bring up the "Macro IDE" window in Visual Studio by pressing Alt+F11.
  • Right-click on MyMacros and select Add Module; alternatively, you can just rename Module1 instead of creating a new module. Enter in GuidModule as the name.
  • Copy and paste the following code between Public Module GuidModule and End Module:
REM Insert a new Guid at whatever the cursor has selected
Sub InsertGuid()
   Dim ts As TextSelection = ActiveDocument().Selection
   Dim guid As System.Guid = System.Guid.NewGuid()
   ts.Insert(guid.ToString().ToUpper())
End Sub
  • Be sure to save the changes and close the "Macro IDE" window.

Assign a Keyboard Combination

  • Back at the main Visual Studio window, go to Tools Options… and then navigate to Environment Keyboard.
  • Type InsertGuid into the Show Commands Containing text field and select the macro you just created (Macros.MyMacros.GuidModule.InsertGuid).
  • Put the cursor in the Press shortcut keys text field and type a keyboard combination (I have opted to use Alt+G).
  • Click on Assign (be careful, as it will replace any existing binding for that key).

Comments

Eyal
great macro thanks