How to generate a GUID in VBScript?

vividos picture vividos · Jun 9, 2009 · Viewed 40.5k times · Source

I want to generate GUID strings in VBScript. I know that there's no built-in function in VBScript for generating one. I don't want to use random-generated GUIDs. Maybe there is an ActiveX object that can be created using CreateObject() that is sure to be installed on (newer) Windows versions that can generate a GUID?

Answer

Helen picture Helen · Jun 9, 2009
Function CreateGUID
  Dim TypeLib
  Set TypeLib = CreateObject("Scriptlet.TypeLib")
  CreateGUID = Mid(TypeLib.Guid, 2, 36)
End Function

This function will return a plain GUID, e.g., 47BC69BD-06A5-4617-B730-B644DBCD40A9.

If you want a GUID in a registry format, e.g., {47BC69BD-06A5-4617-B730-B644DBCD40A9}, change the function's last line to

CreateGUID = Left(TypeLib.Guid, 38)