Excel VBA 在非默认日历中创建会议

Posted

技术标签:

【中文标题】Excel VBA 在非默认日历中创建会议【英文标题】:Excel VBA create Meeting in Non-Default Calendar 【发布时间】: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 【参考方案1】:

此代码将在 Outlook 的非默认帐户中的非默认日历上创建约会。希望这对将来的其他人有所帮助:

Sub Whatever()
Dim olApp As Object
Set olApp = GetObject(, "Outlook.Application")
Dim oApt As Outlook.AppointmentItem
Dim ns As Outlook.Namespace
Dim recip As Outlook.Recipient
Dim oFolder As Outlook.Folder
Set ns = olApp.GetNamespace("MAPI")
Set recip = ns.CreateRecipient("otheremail@contoso.com")

If recip.Resolve Then
    Set otherFolder = ns.GetSharedDefaultFolder(recip, olFolderCalendar)
End If

Set oApt = otherFolder.Items.Add(olAppointmentItem)

oApt.MeetingStatus = olMeeting
    With oApt
        .Subject = "Test"
        .Start = "15/04/2019 09:00"
        .End = "15/04/2019 09:10"
        .Location = "The Business Meeting Room"
        .Recipients.Add ("user@contoso.com")
        .Send
    End With
End Sub

【讨论】:

在尝试设置oApt 之前,您可能需要检查otherFolder 是否为空。如果recip 无法解析,您将收到错误消息。

以上是关于Excel VBA 在非默认日历中创建会议的主要内容,如果未能解决你的问题,请参考以下文章

查看 Google 日历中创建的每个活动的会议链接

如何在PHP中创建Outlook日历会议请求?

是否有 VBA 方法可以在 Outlook 中创建新日历(不是约会)

访问未找到在网络上的 vba 中创建的 excel 文件

如何更改在VBA中创建的下拉列表的字体大小和格式

使用 terraform 在非默认 VPC 中创建 AWS RDS 实例