使用 Excel 数据创建 Outlook 会议请求

发布于 2024-12-17 02:17:54 字数 1360 浏览 0 评论 0原文

我正在编写代码来创建 Outlook 会议请求以发送给受邀者列表。
我在日历中看到会议请求,但无法发送。
我怎样才能让它发送?

Sub AddAppointments()
    ' Create the Outlook session
    Set myOutlook = CreateObject("Outlook.Application")

    ' Start at row 2
    r = 2

    Do Until Trim(Cells(r, 1).Value) = ""
        ' Create the AppointmentItem
        Set myApt = myOutlook.CreateItem(1)
        ' Set the appointment properties
        myApt.Subject = Cells(r, 1).Value
        myApt.Location = Cells(r, 2).Value
        myApt.Start = Cells(r, 3).Value
        myApt.Duration = Cells(r, 4).Value
        myApt.Recipients.Add Cells(r, 8).Value
        myApt.MeetingStatus = olMeeting
        myApt.ReminderMinutesBeforeStart = 88
        myApt.Recipients.ResolveAll
        myApt.AllDayEvent = AllDay

        ' If Busy Status is not specified, default to 2 (Busy)
        If Trim(Cells(r, 5).Value) = "" Then
            myApt.BusyStatus = 2
        Else
            myApt.BusyStatus = Cells(r, 5).Value
        End If
        If Cells(r, 6).Value > 0 Then
            myApt.ReminderSet = True
            myApt.ReminderMinutesBeforeStart = Cells(r, 6).Value
        Else
            myApt.ReminderSet = False
        End If
        myApt.Body = Cells(r, 7).Value
        myApt.Save
        r = r + 1
        myApt.Send
    Loop
End Sub

I am working on code to create an Outlook Meeting Request to send to a list of invitees.
I see the Meeting Request in my Calendar, but I am unable to send it.
How can I get it to send?

Sub AddAppointments()
    ' Create the Outlook session
    Set myOutlook = CreateObject("Outlook.Application")

    ' Start at row 2
    r = 2

    Do Until Trim(Cells(r, 1).Value) = ""
        ' Create the AppointmentItem
        Set myApt = myOutlook.CreateItem(1)
        ' Set the appointment properties
        myApt.Subject = Cells(r, 1).Value
        myApt.Location = Cells(r, 2).Value
        myApt.Start = Cells(r, 3).Value
        myApt.Duration = Cells(r, 4).Value
        myApt.Recipients.Add Cells(r, 8).Value
        myApt.MeetingStatus = olMeeting
        myApt.ReminderMinutesBeforeStart = 88
        myApt.Recipients.ResolveAll
        myApt.AllDayEvent = AllDay

        ' If Busy Status is not specified, default to 2 (Busy)
        If Trim(Cells(r, 5).Value) = "" Then
            myApt.BusyStatus = 2
        Else
            myApt.BusyStatus = Cells(r, 5).Value
        End If
        If Cells(r, 6).Value > 0 Then
            myApt.ReminderSet = True
            myApt.ReminderMinutesBeforeStart = Cells(r, 6).Value
        Else
            myApt.ReminderSet = False
        End If
        myApt.Body = Cells(r, 7).Value
        myApt.Save
        r = r + 1
        myApt.Send
    Loop
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的海 2024-12-24 02:17:54

如果没有示例值行,则很难调试此代码。所以我们只是相信你的话,它是有效的。但我确实稍微修改了代码。

  • 您的代码中有两次 ReminderMinutesBeforeStart 。我删除了第一个,因为它看起来依赖于行数据。
  • 您调用 ResolveAll 方法,但不检查收件人是否已解决。如果它们是电子邮件地址,我就不会打扰。
  • 早期和晚期绑定参考文献混合在一起。例如,您使用 1 而不是 olAppointmentItem,但后来使用 olMeeting 而不是 1。
  • AllDayEvent 属性采用布尔值,但由于您没有声明任何变量,我们无法知道<强>全天的意思。我将其转换为从 I 列读取。另请注意,如果将 AllDayEvent 设置为 True,则无需设置 Duration。

假设有效的输入值,此代码对我有用:

Option Explicit

Sub AddAppointments()

  Dim myoutlook As Object ' Outlook.Application
  Dim r As Long
  Dim myapt As Object ' Outlook.AppointmentItem

  ' late bound constants
  Const olAppointmentItem = 1
  Const olBusy = 2
  Const olMeeting = 1

  ' Create the Outlook session
  Set myoutlook = CreateObject("Outlook.Application")

  ' Start at row 2
  r = 2

  Do Until Trim$(Cells(r, 1).value) = ""
    ' Create the AppointmentItem
    Set myapt = myoutlook.CreateItem(olAppointmentItem)
    ' Set the appointment properties
    With myapt
      .Subject = Cells(r, 1).value
      .Location = Cells(r, 2).value
      .Start = Cells(r, 3).value
      .Duration = Cells(r, 4).value
      .Recipients.Add Cells(r, 8).value
      .MeetingStatus = olMeeting
      ' not necessary if recipients are email addresses
      ' myapt.Recipients.ResolveAll
      .AllDayEvent = Cells(r, 9).value

      ' If Busy Status is not specified, default to 2 (Busy)
      If Len(Trim$(Cells(r, 5).value)) = 0 Then
        .BusyStatus = olBusy
      Else
        .BusyStatus = Cells(r, 5).value
      End If

      If Cells(r, 6).value > 0 Then
        .ReminderSet = True
        .ReminderMinutesBeforeStart = Cells(r, 6).value
      Else
        .ReminderSet = False
      End If

      .Body = Cells(r, 7).value
      .Save
      r = r + 1
      .Send
    End With
  Loop
End Sub

单元格中的示例输入值(包括标题行):

  • A2:我的会议
  • B2:我的办公桌
  • C2:11/25/2011 13:30:00 PM
  • D2:30
  • E2:2
  • F2:30
  • G2:我们开个会吧!
  • H2:-电子邮件地址-
  • I2:FALSE

Without a sample row of values, it's hard to debug this code. So we are only going on your word that it is valid. But I did fix up the code a bit.

  • You have ReminderMinutesBeforeStart twice in your code. I removed the first one because it looks like it is dependent upon row data.
  • You call the ResolveAll method, but don't check to see if your recipients resolved. If they are email addresses, I wouldn't bother.
  • There is a mix of early and late bound references. For example, you use 1 instead of olAppointmentItem, but later use olMeeting instead of 1.
  • The AllDayEvent Property takes a boolean value, but as you haven't declared any variables we have no way to tell what AllDay means. I converted this to read from column I. Also note that if you set AllDayEvent to True, you would not need to set Duration.

Assuming valid input values, this code worked for me:

Option Explicit

Sub AddAppointments()

  Dim myoutlook As Object ' Outlook.Application
  Dim r As Long
  Dim myapt As Object ' Outlook.AppointmentItem

  ' late bound constants
  Const olAppointmentItem = 1
  Const olBusy = 2
  Const olMeeting = 1

  ' Create the Outlook session
  Set myoutlook = CreateObject("Outlook.Application")

  ' Start at row 2
  r = 2

  Do Until Trim$(Cells(r, 1).value) = ""
    ' Create the AppointmentItem
    Set myapt = myoutlook.CreateItem(olAppointmentItem)
    ' Set the appointment properties
    With myapt
      .Subject = Cells(r, 1).value
      .Location = Cells(r, 2).value
      .Start = Cells(r, 3).value
      .Duration = Cells(r, 4).value
      .Recipients.Add Cells(r, 8).value
      .MeetingStatus = olMeeting
      ' not necessary if recipients are email addresses
      ' myapt.Recipients.ResolveAll
      .AllDayEvent = Cells(r, 9).value

      ' If Busy Status is not specified, default to 2 (Busy)
      If Len(Trim$(Cells(r, 5).value)) = 0 Then
        .BusyStatus = olBusy
      Else
        .BusyStatus = Cells(r, 5).value
      End If

      If Cells(r, 6).value > 0 Then
        .ReminderSet = True
        .ReminderMinutesBeforeStart = Cells(r, 6).value
      Else
        .ReminderSet = False
      End If

      .Body = Cells(r, 7).value
      .Save
      r = r + 1
      .Send
    End With
  Loop
End Sub

Sample input values in cells (incl. header row):

  • A2: My Meeting
  • B2: My Desk
  • C2: 11/25/2011 13:30:00 PM
  • D2: 30
  • E2: 2
  • F2: 30
  • G2: Let's have a meeting!
  • H2: -email address-
  • I2: FALSE
渡你暖光 2024-12-24 02:17:54

这对我有用!

请记住有多行,例如

.Recipients.Add Cells(r, 8).value

添加更多收件人。
因为在一个单元格中写入多个地址,并用“;”分隔。发送约会时会导致错误!

或使用

.Recipients.ResolveAll 

It works for me!

Please keep in mind to have multiple lines like

.Recipients.Add Cells(r, 8).value

to add more recipients.
Because writing several addresses in one cell separeted by ";" leads to an error when sendig the appointment!

or use

.Recipients.ResolveAll 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文