通过 VBA 从 Excel 发送电子邮件附件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37302602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending email attachments from Excel via VBA
提问by Davey
I've written a macro that at the click of a button it sends an automated email via Outlook. Everything runs smoothly except I just can't figure out how to attach a file to the email. Everywhere I've looked, example code for attaching files to an email is for static named files, as in, you're sending the same file name, with the same path every time.
我编写了一个宏,只需单击一个按钮,它就会通过 Outlook 发送一封自动电子邮件。一切都运行顺利,只是我不知道如何将文件附加到电子邮件中。我看过的任何地方,用于将文件附加到电子邮件的示例代码都是针对静态命名文件的,例如,您每次都发送相同的文件名和相同的路径。
If it makes it more convenient, the button that runs this macro is inside the workbook that I'm trying to attach. I'm not sure if opening a Windows Explorer window is easiest and attaching the file that way would be best.
如果它更方便,运行此宏的按钮位于我尝试附加的工作簿内。我不确定打开 Windows 资源管理器窗口是否最简单,以这种方式附加文件是否最好。
Sub mySub
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.recipient
Dim objOutlookAttach As Outlook.Attachment
Dim WeekendingDate As Date
With Worksheets("Macro Buttons")
WeekendingDate = Range("N2").Value
End With
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
Set objOutlookRecip = .Recipients.Add("blah@blah")
objOutlookRecip.Type = olTo
.Subject = "Blah " & WeekendingDate
.Body = "blah blah blah"
'Add attachments to the message
[some code]
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
If DisplayMsg Then
.Display
Else
.Save
End If
End With
Set objOutlook = Nothing
End Sub
回答by Dave
You need the Attachments.Addcode inserted into the MailItem setup:
您需要将Attachments.Add代码插入到 MailItem 设置中:
With objOutlookMsg
Set objOutlookRecip = .Recipients.Add("blah@blah")
objOutlookRecip.Type = olTo
.Subject = "Blah " & WeekendingDate
.Body = "blah blah blah"
'Add attachments to the message [some code]
.Attachments.Add "pathToFile"
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
If DisplayMsg Then
.Display
Else
.Save
End If
End With
Set objOutlook = Nothing
In one of my own scripts I pass a collection of attachments to the MailItem to be attached using a Dictionary object and the following code:
在我自己的一个脚本中,我将一组附件传递给要使用 Dictionary 对象和以下代码附加的 MailItem:
With oMailItem
Set .SendUsingAccount = oOutlook.Session.Accounts.Item(iAccount)
.To = EmailData("To")
.CC = EmailData("CC")
.BCC = EmailData("BCC")
.Subject = EmailData("Subject")
.Body = EmailData("Body")
sAttachArray = Split(EmailData("AttachmentPaths"), ";")
For Each sAttachment In sAttachArray
.Attachments.Add(sAttachment)
Next
.Recipients.ResolveAll
.Display ' debug mode - uncomment this to see email before it's sent out
End With

