我的以下代码在 PC 上运行正常,但在 Mac 上不起作用。我希望脚本能够识别当前操作系统并为该操作系统运行适当的命令集,而不是为 Windows 和 Mac 用户制作两个带有单独按钮的宏版本。
该宏创建一封带有工作簿附件的电子邮件。该附件是 ActiveWorkbook 的临时版本,在发送电子邮件后将被删除。
我目前用来发送电子邮件的方法是 Windows CDO。在装有 Office 2016 的 MAC OSX 上执行时,我还应该注意其他注意事项吗?
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") = "[email protected]"
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
因此,为了确定操作系统,您可以使用条件编译指令像这样:
#If Mac Then
Debug.Print "I'm a Mac"
#Else
Debug.Print "I'm not"
#End If
由于操作系统内置的安全性,在现代 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 "[email protected]"
set content to "My email message"
make new to recipient with properties {address:"[email protected]"}
send
end tell
end tell
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)