Wednesday 6 November 2019

How to create a GUID in Visual Studio (and VBA)

I've just reading some comments on other posts. In this post I'll quickly show where the GUID generator is in Visual Studio...

From the Visual Studio's main menu take the Tools menu and midway down that list is Create GUID...

Clicking on 'Create GUID' menu item take you to this dialog box. Press Copy to copy to clipboard, you can select the format using the radio button on the left.

If you want some VBA code to generate the GUID then I found this on StackOverflow

Option Explicit

'* With thanks to StackOverflow
'* https://stackoverflow.com/questions/7031347/how-can-i-generate-guids-in-excel#answer-48434899
'* and specifically user https://stackoverflow.com/users/3056160/rchacko

Declare Function CoCreateGuid Lib "ole32" (ByRef GUID As Byte) As Long
Public Function GenerateGUID() As String
    Dim ID(0 To 15) As Byte
    Dim N As Long
    Dim GUID As String
    Dim Res As Long
    Res = CoCreateGuid(ID(0))

    For N = 0 To 15
        GUID = GUID & IIf(ID(N) < 16, "0", "") & Hex$(ID(N))
        If Len(GUID) = 8 Or Len(GUID) = 13 Or Len(GUID) = 18 Or Len(GUID) = 23 Then
            GUID = GUID & "-"
        End If
    Next N
    GenerateGUID = GUID
End Function

No comments:

Post a Comment