VBA - 从GUID中删除括号{}

时间:2017-07-31 12:56:31

标签: string vba guid

您好我刚刚实施了微软更新后生成GUID代码的新修补程序。我是VBA的新手,请你帮我删除下面代码中用GUID生成的{}大括号。

 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
Public Function GetGUID()
            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

GetGUID = strGuid
                            End If
            End If

结束功能

2 个答案:

答案 0 :(得分:0)

编辑此行。

GetGUID =替换(替换(strGuid,“{”,“”),“}”,“”)

答案 1 :(得分:0)

只需使用Mid即可删除第一个和最后一个字符。 您还可以使用一个字节数组来保存guid和结果字符串。

生成GUID:

Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (pguid As Byte) As Long
Private Declare PtrSafe Function StringFromGUID2 Lib "ole32.dll" (rguid As Byte, lpsz As Byte, ByVal cchMax As Long) As Long


Public Function GenerateGUID() As String
  Dim pguid(0 To 15) As Byte, lpsz(0 To 77) As Byte

  CoCreateGuid pguid(0)
  StringFromGUID2 pguid(0), lpsz(0), 39
  GenerateGUID = Mid$(lpsz, 2, 36)
End Function