![在 Outlook 2010 中延迟会议邀请-vba](https://linux22.com/image/1543923/%E5%9C%A8%20Outlook%202010%20%E4%B8%AD%E5%BB%B6%E8%BF%9F%E4%BC%9A%E8%AE%AE%E9%82%80%E8%AF%B7-vba.png)
我正在尝试编写代码来安排会议邀请并延迟在以后的日期/时间自动发送给参与者,即延迟发送会议邀请
下面是代码,但当我想要 30 分钟后发送邀请时,它出现了错误。
错误行:
应用程序.等待(现在 + 时间值(“06:30:00”))
非常感谢您的帮助。非常感谢
Sub Book_meeting_room()
Dim olApp As Outlook.Application
Dim olApt As AppointmentItem
Set olApp = Outlook.Application 'Creating Outlook Session
Set olApt = olApp.CreateItem(olAppointmentItem) 'Creating an Appointment
With olApt
.MeetingStatus = olMeeting 'olAppointmentItem with Meeting status olMeeting
'becomes a OL Meeting Item.
.Subject = "Room 1" 'Subject
.Start = #11/20/2017 8:30:00 AM# 'Enter Date + Time here.
.Duration = 240 'In Minutes
.Location = "Office" 'Location of the meeting.
.Recipients.Add ("Margaret") 'Recipient Name, Alias, or any other Attribute.
.BusyStatus = olFree
.ReminderSet = True
.ReminderMinutesBeforeStart = 20
End With
Application.Wait (Now + TimeValue("06:30:00")) 'defer 06hrs and 30mins.
olApt.Send 'Sending Mail.
Set olApt = Nothing
MsgBox "Invite Sent", vbInformation
End Sub
答案1
Application.Wait
不适用于 Outlook,您可以使用Doevent
计时器功能,也可以使用Sleep
功能
使用Doevent
是最好的选择,因为它允许您在启动宏后继续工作,如果过度使用它就会出现问题:
Public Sub Pause(Seconds As Single)
Dim TimeEnd As Single
TimeEnd = Timer + Seconds
While Timer < TimeEnd
DoEvents
Wend
End Sub
使用 sleep 时,您需要声明该函数,并且在发送消息之前,它将不允许您继续工作。您可以使用以下命令声明它:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
您的代码如下所示(您还可以注意将要解决的其他问题以.Display
使程序正常运行)
Sub Book_meeting_room()
Dim olApp As Outlook.Application
Dim olApt As AppointmentItem
Set olApp = Outlook.Application 'Creating Outlook Session
Set olApt = olApp.CreateItem(olAppointmentItem) 'Creating an Appointment
With olApt
.MeetingStatus = olMeeting 'olAppointmentItem with Meeting status olMeeting
'becomes a OL Meeting Item.
.Subject = "Room 1" 'Subject
.Start = #11/20/2017 8:30:00 AM# 'Enter Date + Time here.
.Duration = 240 'In Minutes
.Location = "Office" 'Location of the meeting.
.Recipients.Add ("Margaret") 'Recipient Name, Alias, or any other Attribute.
.BusyStatus = olFree
.ReminderSet = True
.ReminderMinutesBeforeStart = 20
.Display
End With
Pause (23400) 'defer 06hrs and 30mins.
'Sleep (23400) 'also defer 06hrs and 30mins eventually
olApt.Send 'Sending Mail.
Set olApt = Nothing
MsgBox "Invite Sent", vbInformation
End Sub