Excel 从Windows和Mac上的VBA宏发送带有工作簿的电子邮件
我的以下代码在PC上正常工作,但在Mac上不工作。我希望脚本能够识别当前操作系统并为该操作系统运行相应的命令集,而不是为Windows和Mac用户制作两个带有单独按钮的宏版本 宏将创建带有工作簿附件的电子邮件。附件是ActiveWorkbook的临时版本,在发送电子邮件后将被删除 我目前用来发送电子邮件的方法是Windows CDO。使用Office 2016在MAC OSX上执行时,我还需要注意其他事项吗Excel 从Windows和Mac上的VBA宏发送带有工作簿的电子邮件,excel,vba,windows,macos,cdo,Excel,Vba,Windows,Macos,Cdo,我的以下代码在PC上正常工作,但在Mac上不工作。我希望脚本能够识别当前操作系统并为该操作系统运行相应的命令集,而不是为Windows和Mac用户制作两个带有单独按钮的宏版本 宏将创建带有工作簿附件的电子邮件。附件是ActiveWorkbook的临时版本,在发送电子邮件后将被删除 我目前用来发送电子邮件的方法是Windows CDO。使用Office 2016在MAC OSX上执行时,我还需要注意其他事项吗 Private Message As CDO.Message Private Attac
Private Message As CDO.Message
Private Attachment, Expression, Matches, FilenameMatch, i
Sub enviar_mail()
Dim wb1 As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb1 = ActiveWorkbook
'Make a copy of the file/Open it/Mail it/Delete it
'If you want to change the file name then change only TempFileName
TempFilePath = Environ$("temp") & "\"
TempFileName = "Copy of " & wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
On Error Resume Next
Set Message = New CDO.Message
Message.Subject = ActiveSheet.Range("G9").Value
Message.From = ""
Message.To = ""
Message.CC = ""
Message.HTMLBody = ActiveSheet.Range("A12").Value
Message.AddAttachment TempFilePath & TempFileName & FileExtStr
Dim Configuration
Set Configuration = CreateObject("CDO.Configuration")
Configuration.Load -1 ' CDO Source Defaults
Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "name@email.com"
Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "*****"
Configuration.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
Configuration.fields.Update
Set Message.Configuration = Configuration
Message.Send
On Error GoTo 0
'Delete the file
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
因此,为了确定操作系统,您可以使用如下方法: 在现代MacOS上发送邮件很棘手,因为操作系统内置了安全性。CDO严格来说是一种Windows技术,在这里不适用。大多数人会编写一个单独的AppleScript文件,然后由Excel执行。有关如何为Outlook和Mail.app执行此操作的详细信息,请参阅 首先,它当然需要额外的步骤才能将脚本输入用户的计算机,但是AppleScript非常容易理解。例如:
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
我希望脚本具有足够的动态性,能够识别操作系统的类型并临时保存工作簿,而不是创建另一个按钮和模块从MAC上执行
您能解释一下原因吗?在我们公司,有使用PC的用户,也有使用MAC的用户。我在考虑使用Application.OperatingSystem,然后基于它返回的内容,使用两个独立的例程,一个为PC构建,一个为OSX构建,适当嵌套在If Else语句中。另外,当我说“暂时保存工作簿”时,我指的是代码实例,它将文件保存到附件的临时位置,然后将其删除。我没有Mac电脑来测试这一点-它当前是否仅在PC上工作,而在Mac上不工作?是的,PC工作正常,我相信这可能与临时文件路径分配一样简单,但使用CDO和windows功能时,我不确定是否必须使用其他工具/方法来使用MAC发送smtp。
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