当您运行 Visual Basic for Applications 调用 CreateObject("Scriptlet.TypeLib") (VBA) 代码。GUID函数来创建 GUID (Word、Excel 等),则 Microsoft Office 应用程序中您会收到以下错误消息:
Run-time error '70':
Permission denied
此问题是由于。在 7 月 2017年安全更新、 CreateObject("Scriptlet.TypeLib")。GUID 被阻止的 Office VBA 中的恶意代码的安全防范。
新的方法是:
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 newGUID()
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
newGUID = strGuid
End If
End If
End Function