启用Outlook的自动回复功能,设定特定日期。

huangapple go评论86阅读模式
英文:

Enable autoreply with certain date for Outlook

问题

这段VBA代码位于Excel中,版本为Office 365。出现的错误是“Type mismatch”,出现在SetProperty xxxxx0X661E001F和0x661F0040处。我尝试将strMessge更改为variant或更改为UNICODE。

英文:

This VBA code is in Excel. The version is Office 365.

The error raised is

>Type mismatch

in SetProperty xxxxx0X661E001F and also 0x661F0040.

I tried to change the strMessge to variant or change to 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

答案1

得分: 0

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

  • 该属性是只读的,因为某些Outlook和MAPI属性是只读的。
  • 指定命名空间引用的属性未找到。
  • 属性以无效格式指定,无法解析。
  • 该属性不存在且无法创建。
  • 该属性存在,但传递了不正确类型的值。
  • 无法打开属性,因为客户端处于离线状态。
  • 使用UserProperties.Add方法创建属性。在首次设置属性时,必须使用PropertyAccessor对象的UserProperty.Value属性,而不是SetPropertiesSetProperty方法。

因此,正如您所见,代码可能失败的原因太多。我建议使用诸如MFCMAPIOutlookSpy之类的低级属性资源管理工具,尝试手动设置这些属性,以确保您尝试分配的值是有效的。如果它们有效,我建议尝试使用VBA确保可以使用SetProperty

英文:

Outlook may apply its own business rules on setting low-level properties using the PropertyAccessor.SetProperty method. Conditions where setting properties fails include:

  • The property is read-only, as some Outlook and MAPI properties are read-only.
  • The property referenced by the specified namespace is not found.
  • The property is specified in an invalid format and cannot be parsed.
  • The property does not exist and cannot be created.
  • The property exists but is passed a value of an incorrect type.
  • Cannot open the property because the client is offline.
  • The property is created using the UserProperties.Add method. When setting the property for the first time, you must use the UserProperty.Value property instead of the SetProperties or SetProperty method of the PropertyAccessor object.

So, as you can see there are too many reasons why the code may fail. I'd suggest playing with any low-level property explorer tool such as MFCMAPI or OutlookSpy and try to set these properties manually to make sure the values you are trying to assign are valid. If they are valid I'd suggest playing with VBA to make sure the SetProperty can be used for that.

答案2

得分: 0

以下是翻译好的部分:

最后两个属性(0x661F00400x66230040)属于 PT_SYSTIME 类型(0x0040),因此您必须传递 DateTime 值,而不是字符串。将数据转换为正确的类型是您的责任。在这种特定情况下,使用 CDate 函数:

.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x661F0040", CDate(strStartDate) '设置自动回复开始日期
.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x66230040", CDate(strEndDate) '设置自动回复结束日期

如果您要设置 OOF 状态和范围,请记住无法使用 MAPI 设置它,您需要使用 EWS。

英文:

The last two properties (0x661F0040 and 0x66230040) are of PT_SYSTIME type (0x0040), therefore you must pass a DateTime value, not a string. It is your responsibility to convert your data to the right type. In this particular case, use CDate function:

.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x661F0040", CDate(strStartDate) 'Set auto-reply start date
.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x66230040", CDate(strEndDate) 'Set auto-reply end date

If you are setting OOF state and range, keep in mind that it cannot be set using MAPI, you need to use EWS for that.

huangapple
  • 本文由 发表于 2023年5月10日 16:42:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定