Created
June 28, 2020 07:56
-
-
Save devjev/ea48c35e499cd8c2bc187bdff4399cce to your computer and use it in GitHub Desktop.
Create GUIDs in Excel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Private Type GUID_TYPE | |
Data1 As Long | |
Data2 As Integer | |
Data3 As Integer | |
Data4(7) As Byte | |
End Type | |
Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (Guid As GUID_TYPE) As LongPtr | |
Private Declare PtrSafe Function StringFromGUID2 Lib "ole32.dll" (Guid As GUID_TYPE, ByVal lpStrGuid As LongPtr, ByVal cbMax As Long) As LongPtr | |
Function CreateGuidString(Optional IncludeHyphens As Boolean = True, Optional IncludeBraces As Boolean = False) | |
Dim Guid As GUID_TYPE | |
Dim strGuid As String | |
Dim retValue As LongPtr | |
Const guidLength As Long = 39 'registry GUID format with null terminator {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} | |
retValue = CoCreateGuid(Guid) | |
If retValue = 0 Then | |
strGuid = String$(guidLength, vbNullChar) | |
retValue = StringFromGUID2(Guid, StrPtr(strGuid), guidLength) | |
If retValue = guidLength Then | |
' valid GUID as a string | |
' 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 | |
CreateGuidString = strGuid | |
End If | |
End If | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment