| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Excel VBA GUIDを生成する」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
(ページの作成:「==Excel VBA GUIDを生成する== ======== Public Sub GetGUID() Dim typelib As Object Dim guid As String Set typelib = CreateObject("Scriptle…」)
 
1行目: 1行目:
==Excel VBA GUIDを生成する==
+
==[[Excel VBA GUIDを生成する]]==
 
========
 
========
 
  Public Sub GetGUID()  
 
  Public Sub GetGUID()  
46行目: 46行目:
 
     strGuid = CreateGuidString()
 
     strGuid = CreateGuidString()
 
      
 
      
     strGuid = Replace(Replace(strGuid, "{", ""), "}", "")
+
     strGuid = [[R]]eplace([[R]]eplace(strGuid, "{", ""), "}", "")
 
      
 
      
 
     ActiveCell.Value = strGuid
 
     ActiveCell.Value = strGuid
 
  End Sub
 
  End Sub

2020年2月16日 (日) 04:25時点における版

Excel VBA GUIDを生成する

==

  1. Public Sub GetGUID()
  2. Dim typelib As Object
  3. Dim guid As String
  4. Set typelib = CreateObject("Scriptlet.TypeLib")
  5. guid = Mid$(typelib.guid, 2, 36)
  6. ActiveCell.Value = guid
  7. End Sub

上記でエラーとなる場合

  1. Private Const SEP As String = ","
  1. Private Type GUID_TYPE
  2. Data1 As Long
  3. Data2 As Integer
  4. Data3 As Integer
  5. Data4(7) As Byte
  6. End Type
  7. Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (guid As GUID_TYPE) As LongPtr
  8. Private Declare PtrSafe Function StringFromGUID2 Lib "ole32.dll" (guid As GUID_TYPE, ByVal lpStrGuid As LongPtr, ByVal cbMax As Long) As LongPtr
  9. Function CreateGuidString()
  10. Dim guid As GUID_TYPE
  11. Dim strGuid As String
  12. Dim retValue As LongPtr
  13. Const guidLength As Long = 39 'registry GUID format with null terminator {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
  14. retValue = CoCreateGuid(guid)
  15. If retValue = 0 Then
  16. strGuid = String$(guidLength, vbNullChar)
  17. retValue = StringFromGUID2(guid, StrPtr(strGuid), guidLength)
  18. If retValue = guidLength Then
  19. ' valid GUID as a string
  20. CreateGuidString = strGuid
  21. End If
  22. End If
  23. End Function
  24. Public Sub GetGUID()
  25. Dim strGuid As String
  26. strGuid = CreateGuidString()
  27. strGuid = Replace(Replace(strGuid, "{", ""), "}", "")
  28. ActiveCell.Value = strGuid
  29. End Sub