为什么对象在Variant不会出现的地方引发错误?

时间:2019-04-16 14:36:36

标签: excel vba outlook outlook-vba

从MSDocs复制的以下代码在我进行细微修改后引发错误。最初,绑定是早期而不是后期。但是,所有类型(NamespaceSharingItem)都是对象的类型。但是,如下面的示例中所示修改代码后,它将引发错误:

错误号-> -2147467259

错误说明->操作失败

错误源-> Microsoft Outlook

Sub MSCodeTest()

 Dim oApp As Object           'Application --> added by me
 Dim oNamespace As Object     'Namespace
 Dim oFolder As Object        'Folder
 Dim oSharingItem As Object   'SharingItem
 On Error GoTo ErrRoutine
 Set oApp = GetObject(, "Outlook.Application")   'added by me
 Set oNamespace = oApp.GetNamespace("MAPI")
 Set oFolder = oNamespace.GetDefaultFolder(olFolderCalendar)
 Set oSharingItem = oNamespace.CreateSharingItem(oFolder)
 oSharingItem.Display

EndRoutine:

 On Error GoTo 0
 Set oSharingItem = Nothing
 Set oFolder = Nothing
 Set oNamespace = Nothing
Exit Sub

ErrRoutine:

 MsgBox Err.Description, vbOKOnly, Err.Number & " - " & Err.source
 GoTo EndRoutine

End Sub

另一方面,如果将Folder从Object更改为Variant,一切都会顺利进行。为什么将Folder之类的对象类型项分配给Object并引发错误,而分配给Variant却不会出错?

0 个答案:

没有答案