我想更改默认的延迟发送选项,以便如果我在周末发送,我有一个快速链接可以延迟到星期一早上 6:30 或 7:00;如果我在工作日(周一至周四)晚上发送,我可以选择延迟发送,延迟发送快速访问工具栏的默认设置是第二天早上 6:30 或 7 点左右。
我按照这篇文章中概述的所有步骤创建了宏: Outlook:更改“在此之前不传送”功能的默认电子邮件延迟时间
但是当我运行宏时收到此错误:
第 0 行发生错误,描述:未设置对象变量或块变量,错误编号为 91。
我怎样才能解决这个问题?
答案1
我想要的解决方案和你基本一样,但那个宏也遇到了一些问题。我搜索了其他几个网站,最终找到了此解决方案这让我大体上完成了任务。我稍微修改了一下他们的宏,最终得到了下面的代码。我只在“新消息”的快速访问工具栏上添加了一个按钮来运行宏,并且还没有测试原始代码的错误处理,所以我不能保证这一点,但我保留了它。
我对原作做了以下改动:
- 将与 NoDeferredDelivery 默认时间的比较更改为不相等,因为我只想在已设置延迟的情况下删除延迟。
- 在该 If 块中添加了 Exit Sub,否则它将继续并无论如何都会延迟。
- 修改了日期计算逻辑,这样如果是周日至周四,我的默认发送时间就是第二天早上 7:38,如果是周五或周六,则默认发送时间就是周一早上。
- 更新了告诉您新发送时间的消息框,包括星期几,这样如果您在周五或周六运行它,就会清楚地知道它会转到周一而不是明天早上。
Sub DelaySendMail()
On Error GoTo ErrHand ' Error Handling
Dim objMailItem As MailItem ' Object to hold mail item
Dim SendDate As String ' The date to send delayed mail
Dim SendTime As String ' The time to send delayed mail
Dim MailIsDelayed As Boolean ' Set if the mail will be delayed
Dim NoDeferredDelivery As String ' Value if deferred delivery is disabled
SendTime = " 07:38:00" ' Time to deliver delayed mail (7:38 AM)
MailIsDelayed = False ' We assume it's being delivered now
NoDeferredDelivery = "1/1/4501" ' Magic number Outlook uses for "delay mail box isn't checked"
'Set object to mail item you have open
Set objMailItem = Outlook.ActiveInspector.CurrentItem
' Check and make sure current item is an unsent message
If objMailItem.Sent = True Then
Err.Raise 9000
End If
' If mail is currently delayed, remove the delay and quit
If objMailItem.DeferredDeliveryTime NoDeferredDelivery Then 'Altered since test wasn't working in original
objMailItem.DeferredDeliveryTime = NoDeferredDelivery
MsgBox "Mail will be delivered immediately when sent", _
vbOKOnly, "Deferred delivery removed"
Exit Sub 'Added since otherwise it didn't actually quit the method
End If
' Set the date appropriately for the next weekday
If Weekday(Date, vbMonday) = 5 Then
' Today is Friday
' Delay mail three days
SendDate = Date + (3)
MailIsDelayed = True
ElseIf Weekday(Date, vbMonday) = 6 Then
' Today is Saturday
' Delay mail two days
SendDate = Date + (2)
MailIsDelayed = True
Else
' Today is Sunday-Thursday so delay till tomorrow morning
SendDate = Date + (1)
MailIsDelayed = True
End If
If MailIsDelayed Then
' Mail should be delayed - set the delivery date/time
objMailItem.DeferredDeliveryTime = SendDate & SendTime
'Altered to include day it would be sent for visiblity over a weekend
MsgBox "Mail will be delivered on " & _
WeekdayName(Weekday(SendDate)) & ", " & SendDate & " at" & SendTime, _
vbOKOnly, "Mail delayed"
End If
Exit Sub
ErrHand:
' Handle well-known errors with message
' Other errors, just tell the user
If Err.Number = 13 Then
' No current item or current item isn't a mail message
MsgBox "Future delivery can only be set on mail items", vbOKOnly, "Not a mail item"
ElseIf Err.Number = 9000 Then
' The active message has already been sent
MsgBox "Please run this macro from an unsent mail item", vbOKOnly, "Not an unsent mail item"
Else
MsgBox "An error has occured on line " & Erl & _
", with a description: " & Err.Description & _
", and an error number " & Err.Number
End If
End Sub