Send email with workbook from VBA macro on both Windows and Mac

天大地大妈咪最大 提交于 2019-12-02 03:30:44

So for determining the OS, you can use conditional compilation directives like so:

#If Mac Then
    Debug.Print "I'm a Mac"
#Else
    Debug.Print "I'm not"
#End If

Sending mail is tricky on modern MacOS because of the security built in to the OS. CDO is strictly a Windows technology and does not apply here. Most people go with writing a separate AppleScript file that is then executed by Excel. See this page for details on how to do it for both Outlook and Mail.app.

It does of course involve extra steps to get the script into the user's computer in the first place, but AppleScript is pretty straightforward to understand. For example:

tell application "Mail"
    set NewMail to (make new outgoing message with properties {subject:"My Subject"})
    tell NewMail
        set sender to "user@example.com"
        set content to "My email message"
        make new to recipient with properties {address:"someone@example.com"}
        send
    end tell
end tell
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!