此 VBA 代码在 Excel 中。版本是Office 365。
抛出的错误是
类型不匹配
在 SetProperty xxxxx0X661E001F 和 0x661F0040 中。
我尝试将 strMessge 更改为变体或更改为 UNICODE。
Option Explicit
Sub SetAutoReply()
Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.Namespace
Dim objStore As Outlook.Store
Dim objPropertyAccessor As Outlook.propertyAccessor
Dim strStartDate As String, strEndDate As String
Dim dtStartDate As Date, dtEndDate As Date
Dim strMessage As String
' Set the auto-reply start and end dates and times
dtStartDate = "05/16/2023 08:00:00" ' Set the start date and time (MM/DD/YYYY HH:MM:SS)
dtEndDate = "05/16/2023 17:00:00" ' Set the end date and time (MM/DD/YYYY HH:MM:SS)
strMessage = "I am currently out of the office and will return on [end_date]."
' Initialize Outlook
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set objOutlook = CreateObject("Outlook.Application")
End If
On Error GoTo 0
' Get the default mailbox
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objStore = objNamespace.DefaultStore
Set objPropertyAccessor = objStore.PropertyAccessor
' Set the auto-reply settings
With objPropertyAccessor
strStartDate = Format(dtStartDate, "yyyy-mm-dd\THH:MM:ss")
strEndDate = Format(dtEndDate, "yyyy-mm-dd\THH:MM:ss")
.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x661D000B", True 'Enable auto-reply
.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x661E001F", strMessage 'Set auto-reply message
.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x661F0040", strStartDate 'Set auto-reply start date
.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x66230040", strEndDate 'Set auto-reply end date
End With
' Release the objects
Set objPropertyAccessor = Nothing
Set objStore = Nothing
Set objNamespace = Nothing
Set objOutlook = Nothing
MsgBox "Auto-reply has been set from " & dtStartDate & " to " & dtEndDate & ".", vbInformation, "Auto-reply Set"
End Sub
Outlook 可以在使用 PropertyAccessor.SetProperty 方法设置低级属性时应用自己的业务规则。设置属性失败的情况包括:
UserProperties.Add
方法创建的。第一次设置属性时,必须使用UserProperty.Value
属性,不能使用SetProperties
对象的SetProperty
或PropertyAccessor
方法因此,如您所见,代码可能失败的原因太多了。我建议使用任何低级属性浏览器工具,例如
MFCMAPI
或 OutlookSpy
并尝试手动设置这些属性以确保您尝试分配的值有效。如果它们有效,我建议使用 VBA 来确保 SetProperty
可以用于此目的。