为 Outlook 启用特定日期的自动回复

问题描述 投票:0回答:1

此 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
excel vba outlook office-automation mapi
1个回答
0
投票

Outlook 可以在使用 PropertyAccessor.SetProperty 方法设置低级属性时应用自己的业务规则。设置属性失败的情况包括:

  • 该属性是只读的,因为某些 Outlook 和 MAPI 属性是只读的。
  • 找不到指定命名空间引用的属性。
  • 指定的属性格式无效,无法解析
  • 该属性不存在,无法创建。
  • 该属性存在但传递的值类型不正确。
  • 客户离线,无法打开房源
  • 属性是使用
    UserProperties.Add
    方法创建的。第一次设置属性时,必须使用
    UserProperty.Value
    属性,不能使用
    SetProperties
    对象的
    SetProperty
    PropertyAccessor
    方法

因此,如您所见,代码可能失败的原因太多了。我建议使用任何低级属性浏览器工具,例如

MFCMAPI
OutlookSpy
并尝试手动设置这些属性以确保您尝试分配的值有效。如果它们有效,我建议使用 VBA 来确保
SetProperty
可以用于此目的。

© www.soinside.com 2019 - 2024. All rights reserved.