简体   繁体   中英

Automate Outlook mail scheduler

I came across a code in Excel VBA which sends mail via Outlook based on given time ranges in the code. The "TO, CC, Subject and Mail Body" are all input in excel only.

The code works fine but .Send giving error 287-Application or object defined error . The code works well in my colleague's laptop, so I am guessing it is some setting error in my outlook or excel. I keep my outlook open while executing the code. The code is written below. Can anyone point to what might be wrong?

Sub Send_Email()
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets("Sheet1")
    
    '''''''''' Update Next ''''''''''''''

    Call Update_Next_Schedule_Time
    Application.OnTime sh.Range("K20").Value, "Send_Email"

    ''''''''''''''''''''''''''''''''''''''
    Dim oa As Object
    Dim msg As Object

    Set oa = CreateObject("outlook.application")
    Set msg = oa.createitem(0)

    With msg
        .To = sh.Range("C2").Value
        .CC = sh.Range("C4").Value
        .Subject = sh.Range("C6").Value
        .Body = sh.Range("C8").Value
        .display
        .send
    End With
End Sub

I believe this is due to your Outlook security policy. Check if it allows programmatic access. If not, and you cannot convince your IT department to make any change, then you should automate the opened outlook app rather that creating a new Outlook instance. Also, are you trying to send the email to yourself, by any chance?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM