如何在Mac版Excel VBA中实现邮件发送?原Outlook方案无法兼容
Mac端Excel VBA发送邮件的替代方案
Mac版Excel确实没法直接用Windows上的CreateObject("Outlook.Application")调用Outlook,但有两种可行的替代方案,取决于你想用Outlook还是Mac自带的Mail应用:
1. 调用Mac版Outlook(需预先安装Outlook)
Mac版Excel的VBA可以通过执行AppleScript来调用Outlook,示例代码如下:
Sub SendEmailWithOutlookOnMac() Dim appleScriptCode As String Dim recipient As String Dim subject As String Dim body As String ' 设置邮件参数 recipient = "example@domain.com" subject = "测试邮件" body = "这是Mac端Excel发送的测试邮件" ' 构造AppleScript代码 appleScriptCode = "tell application ""Microsoft Outlook""" & vbNewLine & _ "set newMail to make new outgoing message with properties {subject:""" & subject & """, content:""" & body & """}" & vbNewLine & _ "make new recipient at newMail with properties {email address:{address:""" & recipient & """}}" & vbNewLine & _ ' 如需添加附件,取消下面一行注释并替换路径 ' "make new attachment at newMail with properties {file name:""/Users/你的用户名/Desktop/test.pdf""}" & vbNewLine & _ "send newMail" & vbNewLine & _ "end tell" ' 执行AppleScript On Error Resume Next MacScript(appleScriptCode) On Error GoTo 0 End Sub
2. 调用Mac自带的Mail应用
如果不想依赖Outlook,也可以直接调用系统自带的Mail,同样用AppleScript实现:
Sub SendEmailWithMacMail() Dim appleScriptCode As String Dim recipient As String Dim subject As String Dim body As String ' 设置邮件参数 recipient = "example@domain.com" subject = "测试邮件" body = "这是Mac端Excel通过系统Mail发送的测试邮件" ' 构造AppleScript代码 appleScriptCode = "tell application ""Mail""" & vbNewLine & _ "set newMessage to make new outgoing message with properties {subject:""" & subject & """, content:""" & body & """, visible:true}" & vbNewLine & _ "make new to recipient at newMessage with properties {address:""" & recipient & """}" & vbNewLine & _ ' 如需添加附件,取消下面一行注释并替换路径 ' "make new attachment at newMessage with properties {file name:""/Users/你的用户名/Desktop/test.pdf""}" & vbNewLine & _ "send newMessage" & vbNewLine & _ "end tell" ' 执行AppleScript On Error Resume Next MacScript(appleScriptCode) On Error GoTo 0 End Sub
注意事项
- 首次运行时,系统会弹出权限请求,需要允许Excel访问邮件应用;
- 路径要使用Mac的绝对路径格式(比如
/Users/xxx/...),不要用Windows的盘符格式; - 如果需要处理多个收件人、抄送或密送,可以在AppleScript里添加对应的
cc recipient或bcc recipient代码块。
内容的提问来源于stack exchange,提问作者RGriffiths
相关产品推荐
相关产品推荐

