How to create a automatic reply with VBA

Jing_li 0 Reputation points

Hi guys,

I used the code which generated by OPENAI about activate Outlook's Autoreply on a certain date in VBA. Somehow it does not work due to some method or property does not support, but I don't know how to fix it.
Can anyone here help to check this?

The error happened in the below statement with the message: Type mismatch.

oPropertyAccessor.SetProperty "", autoReplyMessage
Sub SetAutomaticReply()

    Dim olApp As Object
    Dim oNamespace As Object
    Dim oStore As Object
    Dim oPropertyAccessor As Object

    Dim autoReplyMessage As String
    Dim startDate As Date
    Dim endDate As Date

    ' Set the start and end dates for the automatic reply
    startDate = #5/10/2023#
    endDate = #5/12/2023#

    autoReplyMessage = "I am out of the office from " & Format(startDate, "dddd, mmmm d, yyyy") & " to " & Format(endDate, "dddd, mmmm d, yyyy") & ". I will respond to your email as soon as possible."

    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If Err.Number <> 0 Then
        Set olApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0

    Set oNamespace = olApp.GetNamespace("MAPI")
    Set oStore = oNamespace.DefaultStore
    Set oPropertyAccessor = oStore.PropertyAccessor

    ' Enable automatic replies
    oPropertyAccessor.SetProperty "", True

    ' Set the automatic reply message
    oPropertyAccessor.SetProperty "", autoReplyMessage

End Sub
A family of Microsoft email and calendar products.
3,233 questions
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,610 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,652 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jing_li 0 Reputation points

    My Office version is Microsoft 365 for enterprise.

    0 comments No comments