I am trying to use the alternative to CreateObject("Scriptlet.TypeLib").GUID
provided on this Microsoft support page (with some modification) but get "Compile error: Only comments may appear after End Sub, End Function, or End Property" on the Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (guid As GUID_TYPE) As LongPtr
line.
As can be seen in the code below, this line is within a function so I don't understand what is causing this compile error.
I tried to move the two Private Declare
lines around within the function to see if that would solve the issue but continued to get the same error.
Public Function GetGUID() As String
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
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
GetGUID = strGuid
End If
End If
End Function
This function is being used within a module meant to copy calendar events from the default calendar to another specified calendar.
This code can be found here. Again, it replaces the GetGUID = Mid$(CreateObject("Scriptlet.TypeLib").GUID, 2, 36)
line within this code.
What is causing this error and is there a solution?
答案 0 :(得分:4)
Declare
语句属于模块级别。剪掉这两行并将其移动到模块的顶部,即Option Explicit
的正下方。
编译错误有点笨拙/误导性:如果在模块的两个过程之间有Declare
语句或变量声明 ,则会产生相同的编译错误。
Option Explicit
'legal here
Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (guid As GUID_TYPE) As LongPtr
Public Sub Foo()
End Sub
'illegal here
Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (guid As GUID_TYPE) As LongPtr
Private Function Bar()
'illegal here
Private Declare PtrSafe Function CoCreateGuid Lib "ole32.dll" (guid As GUID_TYPE) As LongPtr
End Sub
Declare
语句必须位于模块的(declarations)
部分中-观察代码窗格窗口顶部的左上角下拉菜单:如果未显示(declarations)
,则您在程序范围内; Declare
语句不能在过程级别范围内。