This function returns a GUID (globally unique identifier) primarily for use in VBA scripts
Option Explicit | |
Public Function CreateGUID(Optional IncludeHyphens As Boolean = True, _ | |
Optional IncludeBraces As Boolean = False) _ | |
As String | |
Dim obj As Object | |
Dim strGUID As String | |
'Late-bind obj as a TypeLib -- a rare time when late-binding | |
'is actually a must! | |
' | |
'No matter how much experimenting I did, the Scriptlet object | |
'could simply not be pummeled into creating a GUID without | |
'a run-time error... See more discussion here: | |
' | |
'https://groups.google.com/forum/#!topic/microsoft.public.scripting.wsh/DWUq-tRLsOo | |
' | |
'All the way back to '99! | |
Set obj = CreateObject("Scriptlet.TypeLib") | |
'Assign the raw GUID, minus the last two erroneous chars | |
strGUID = Left(obj.GUID, Len(obj.GUID) - 2) | |
'If IncludeHyphens is switched from the default True to False, | |
'remove them from the GUID | |
If Not IncludeHyphens Then | |
strGUID = Replace(strGUID, "-", vbNullString, Compare:=vbTextCompare) | |
End If | |
'If IncludeBraces is switched from the default False to True, | |
'leave those curly braces be! | |
If Not IncludeBraces Then | |
strGUID = Replace(strGUID, "{", vbNullString, Compare:=vbTextCompare) | |
strGUID = Replace(strGUID, "}", vbNullString, Compare:=vbTextCompare) | |
End If | |
CreateGUID = strGUID | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment