使用VBA在Outlook电子邮件中附加文件
问题描述:
我创建了一个子例程,它抓取所有相关的详细信息和附件,为我发送自动发送的电子邮件。这里是我的代码:使用VBA在Outlook电子邮件中附加文件
Sub Mail_workbook_Outlook_1()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim To1 As String, CC1 As String, BCC1 As String, Title1 As String, Body1 As String, Att1 As String
' Create "Other Distribution - In Email" Emails
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
To1 = Cells(8, 2).Value
CC1 = Cells(8, 3).Value
BCC1 = Cells(8, 4).Value
Title1 = Cells(8, 5).Value
Body1 = Cells(8, 6).Value
Att1 = Cells(8, 7).Value
On Error Resume Next
With OutMail
' BodyFormat command makes the email a rich format message allowing us to place attachments within the body of the email instead of in the attachment header
.BodyFormat = olFormatRichText
.To = To1
.CC = CC1
.BCC = BCC1
.Subject = Title1
.Body = Body1
.Attachments.Add Att1, , 10
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
这工作得很好,在我的电子邮件的主体的末尾插入我的执着。问题是这个特定的行:
.Attachments.Add Att1, , 10
在这段代码中,“10”应该表示附件的位置。 I.E.这应该表示在我的“Body1”变量的前9个字符之后,而不是第10个字符的附件将被放置在这里。看到这里:https://msdn.microsoft.com/en-us/library/office/ff869553.aspx
但是,由于某种原因,无论我在位置选项中输入什么数字,它总是会将我的附件放在电子邮件的最后。我需要附件是在几段中间,所以这是一个问题。任何想法是什么造成这个?
我应该提到我从工具>参考中选择了Microsoft Outlook对象库。
任何帮助,非常感谢!
答
于是我发现,这是在Outlook 2008/2010的错误那里似乎为这不是一个固定:(
,这似乎http://argurosblog.blogspot.com/2011/11/how-to-create-task-or-appointment-using.html
答
改变车身与内容:
Body1 = "This is the body of the mail, line 1" & vbcrlf
Body1 = Body1 & "This is the second line of text, line 2" & vbcrlf
Body1 = Body1 & "This is the last line of text, line 3."
和运行代码。
正如您可以看到附件未放置在第10.个字符之后,而是在第10.th个字符之后找到第一个vbcrlf
之后。
如果你尝试用.Attachments.Add Att1, , 50
(第二行的中间),它将被放置线2和第3行
如果您删除在你身上所有的vbcrlf
的人物之间,将放置在身体的尽头,那可能是你发生的事情。
解析您身体的内容并在需要的地方插入vbcrlf
('硬返回')字符。
希望这会有所帮助。
很抱歉的延迟反应不它仍然会把附件放在电子邮件的末尾,出于某种原因,我试着玩弄位置参数并试着#1-3,35-40,50和100.所有这些都通过附件在电子邮件的最后... – YASPLS
@YASPLS您使用的是什么版本的Outlook?这已经过Outlook2013的测试,并按照描述的方式运行。 – Cisco
思科,感谢您的帮助。请参阅我的其他帖子。展望2 010,显然这是一个没有修复的已知错误。 – YASPLS