【发布时间】:2019-08-26 19:29:07
【问题描述】:
如何使用 VBA 代码在 Outlook 中非默认电子邮件地址的非默认日历上创建会议?
我在默认电子邮件地址的默认日历中创建邀请的代码:
Sub CreateAppointmentOutlook()
Dim oApp As Outlook.Application
Dim oApt As Outlook.AppointmentItem
Dim oRecip As Outlook.Recipient
Dim i As Long
Dim lastRow As Long
Dim ws As Worksheet
Dim wb As ThisWorkbook
Set oApp = New Outlook.Application
Set ws = ActiveWorkbook.Worksheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 1 To lastRow
Set oApt = oApp.CreateItem(olAppointmentItem)
oApt.MeetingStatus = olMeeting
Debug.Print (ws.Cells(i, 1).Value)
With oApt
.Subject = "Test"
' do some other stuff
End With
Next i
End Sub
我什至尝试更改日历最接近的是reference。为了开始尝试在我的示例中实现此代码,我做了以下测试
Sub Whatever()
Dim olApp As Object
Set olApp = GetObject(, "Outlook.Application")
Dim ns As Outlook.Namespace
Set ns = olApp.GetNamespace("MAPI")
Dim Items As Object
Set Items = GetFolderPath("otheremail@contoso.com\Calendar").Items
Debug.Print (Items.Parent.FolderPath)
Debug.Print ("End")
End Sub
但我得到一个运行时错误“91”:对象变量或块变量未在线设置 Set Items = GetFolderPath("otheremail@contoso.com\Calendar").Items
更新
这段代码运行:
Sub Whatever()
Dim olApp As Object
Set olApp = GetObject(, "Outlook.Application")
Dim oApt As Outlook.AppointmentItem
Dim ns As Outlook.Namespace
Dim oFolder As Outlook.Folder
Set ns = olApp.GetNamespace("MAPI")
Set oFolder = ns.Folders("otheremail@contoso.com")
Dim CalItems As Outlook.Items
Set CalItems = oFolder.Items
End Sub
但是如何在这个其他 CalItems 文件夹集合上创建一个日历条目?
【问题讨论】:
-
当你
Dim as Object时,你需要使用'CreateObject("Library.Member")'。另外,如果你使用items,那是一个集合,而不是一个对象。请参考this。 -
@JVBA 谢谢 - 但我需要更多关于如何声明“项目”的解释。我得到了晚期与早期的绑定。暗淡项目作为集合?
-
我想说,因为每个
MAPI代表您客户端(例如 Outlook)中的每个电子邮件帐户,所以您可以使用Dim fld as Outlook.Folder。然后,Set fld = ns.Folders("otheremail@here.com\Calendar")。然后说,Dim CalItems As Outlook.Items&Set CalItems = fld.Items