使用 创建GUID 代码来实现绝对不会重复: Option ExplicitPrivate Declare Function CoCreateGuid Lib "ole32.dll" (ByRef pguid As GUID) As LongPrivate Type GUID Data1 As Long Data4(0 To 7) As Byte Data3 As Integer Data2 As Integer End Type Private Sub Command1_Click() MsgBox GetGUID End Sub Public Function GetGUID() As String Dim lRetVal As Long Dim udtGuid As GUID
Dim sPartOne As String Dim sPartTwo As String Dim sPartThree As String Dim sPartFour As String Dim iDataLen As Integer Dim iStrLen As Integer Dim iCtr As Integer Dim sAns As String
Option ExplicitPrivate Declare Function CoCreateGuid Lib "ole32.dll" (ByRef pguid As GUID) As LongPrivate Type GUID
Data1 As Long
Data4(0 To 7) As Byte
Data3 As Integer
Data2 As Integer
End Type
Private Sub Command1_Click()
MsgBox GetGUID
End Sub
Public Function GetGUID() As String
Dim lRetVal As Long
Dim udtGuid As GUID
Dim sPartOne As String
Dim sPartTwo As String
Dim sPartThree As String
Dim sPartFour As String
Dim iDataLen As Integer
Dim iStrLen As Integer
Dim iCtr As Integer
Dim sAns As String
On Error GoTo errorhandler
sAns = ""
lRetVal = CoCreateGuid(udtGuid)
If lRetVal = 0 Then
'First 8 chars
sPartOne = Hex$(udtGuid.Data1)
iStrLen = Len(sPartOne)
iDataLen = Len(udtGuid.Data1)
sPartOne = String((iDataLen * 2) - iStrLen, "0") _
& Trim$(sPartOne)
'Next 4 Chars
sPartTwo = Hex$(udtGuid.Data2)
iStrLen = Len(sPartTwo)
iDataLen = Len(udtGuid.Data2)
sPartTwo = String((iDataLen * 2) - iStrLen, "0") _
& Trim$(sPartTwo)
'Next 4 Chars
sPartThree = Hex$(udtGuid.Data3)
iStrLen = Len(sPartThree)
iDataLen = Len(udtGuid.Data3)
sPartThree = String((iDataLen * 2) - iStrLen, "0") _
& Trim$(sPartThree) 'Next 2 bytes (4 hex digits)
'Final 16 chars
For iCtr = 0 To 7
sPartFour = sPartFour & _
Format$(Hex$(udtGuid.Data4(iCtr)), "00")
Next 'To create GUID with "-", change line below to:
'sAns = sPartOne & "-" & sPartTwo & "-" & sPartThree _
'& "-" & sPartFour
sAns = sPartOne & sPartTwo & sPartThree & sPartFour
End If
GetGUID = sAns
Exit Function
errorhandler:
'return a blank string if there's an error
Exit Function
End Function