我在 Outlook VBA 中使用此代码来检查和设置自动回复功能状态。当我手动激活 Outlook 中的功能并通过 VBA 检查状态时,我每次都会得到预期的值。
但是当我通过 VBA 设置该值时,我还可以通过 VBA 检查它是否设置为 True,但该功能在 Outlook 中看起来未激活。我尝试从另一个帐户发送电子邮件以查看是否收到自动回复,但没有收到自动回复。
Sub CheckStatus()
MsgBox Check_Out_Of_Office()
End Sub
Sub ActivateStatus()
OutOfOffice True
End Sub
Function Check_Out_Of_Office() As Boolean
'Checks to see if out of office is already enabled
On Error GoTo eh
Dim oNS As Outlook.NameSpace
Dim oStores As Outlook.Stores
Dim oStr As Outlook.Store
Dim oPrp As Outlook.PropertyAccessor
Set oNS = Outlook.GetNamespace("MAPI")
Set oStores = oNS.Stores
For Each oStr In oStores
If oStr.ExchangeStoreType = olPrimaryExchangeMailbox Then
Set oPrp = oStr.PropertyAccessor
Check_Out_Of_Office = oPrp.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x661D000B")
End If
Next
Exit Function
eh:
MsgBox "The following error occurred: " & Err.Description
End Function
Sub OutOfOffice(bolState As Boolean)
'Calling this with a state of True enables out of office and calling it with a state of False disables out of office
On Error GoTo eh
Const PR_OOF_STATE = "http://schemas.microsoft.com/mapi/proptag/0x661D000B"
Dim olkIS As Outlook.Store, olkPA As Outlook.PropertyAccessor
For Each olkIS In Session.Stores
If olkIS.ExchangeStoreType = olPrimaryExchangeMailbox Then
Set olkPA = olkIS.PropertyAccessor
olkPA.SetProperty PR_OOF_STATE, bolState
End If
Next
Set olkIS = Nothing
Set olkPA = Nothing
Exit Sub
eh:
MsgBox "The following error occurred: " & Err.Description
End Sub
我在以下位置找到了此代码:https://4sysops.com/archives/automate-out-of-office-messages-in-outlook-with-visual-basic-for-applications-vba/
我有使用 Microsoft 365 的 Exchange 帐户
我不想安装 Outlook 中未包含的任何外部库,例如 Redemption。
虽然
PR_OOF_STATE
可用于读取OOF状态,但设置它需要SetUserOofSettings
EWS方法。请参阅https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/hh532556(v=exchg.80)