简体   繁体   English

使用VBA在Outlook电子邮件中附加文件

[英]Using VBA to attach a file in an outlook email

I've created a subroutine that grabs all the relevant details and attachments to send out automated emails for me. 我创建了一个子例程,该例程捕获所有相关的详细信息和附件,以便为我发送自动电子邮件。 Here is the code I have: 这是我的代码:

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

This works fine and inserts my attachment at the end of my email body. 这可以正常工作,并将我的附件插入我的电子邮件正文末尾。 The issue is this specific line: 问题是此特定行:

.Attachments.Add Att1, , 10

In this code the "10" is supposed to represent the position of the attachment. 在此代码中,“ 10”应表示附件的位置。 IE this is supposed to mean that after the first 9 characters in my "Body1" variable, instead of the 10th character the attachment will be placed here. IE的意思是,在我的“ Body1”变量的前9个字符之后,而不是第10个字符将附件放在此处。 See here: https://msdn.microsoft.com/en-us/library/office/ff869553.aspx 参见此处: https : //msdn.microsoft.com/zh-cn/library/office/ff869553.aspx

However, for some reason no matter what number I put in the position option it always just puts my attachment at the very end of the email. 但是,由于某种原因,无论我在头寸选项中输入多少数字,它始终只会将附件放在电子邮件的末尾。 I need the attachment to be in the middle of several paragraphs so this is causing an issue. 我需要将附件放在几段的中间,所以这引起了问题。 Any idea what is causing this? 知道是什么原因造成的吗?

I should mention I have selected the Microsoft Outlook Object Library from Tools>References. 我应该提到我已经从“工具”>“参考”中选择了Microsoft Outlook对象库。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

So I found out that this is a bug in Outlook 2008/2010 for which there does not seem to be a fix :( 因此,我发现这是Outlook 2008/2010中的错误,似乎没有修复程序:(

http://argurosblog.blogspot.com/2011/11/how-to-create-task-or-appointment-using.html http://argurosblog.blogspot.com/2011/11/how-to-create-task-or-appointment-using.html

Change the content of the body with: 使用以下方法更改身体的内容:

    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."

and run your code. 并运行您的代码。

As you can see the attachment is not placed after the 10.th character, but after the first vbcrlf found after the 10.th character. 如您所见,附件不是放在第10个字符之后,而是放在第10个字符之后的第一个vbcrlf之后。

If you try with .Attachments.Add Att1, , 50 (in the middle of the second line), it will be placed between line 2 and line 3. 如果尝试使用.Attachments.Add Att1, , 50 (在第二行的中间),它将被放置在第2行和第3行之间。

If you delete all the vbcrlf s characters in your body, it will placed at the end of the body, and that is probably what happens to you. 如果删除vbcrlf的所有vbcrlf字符,它将放置在正文的末尾,这可能就是您所vbcrlf的。

Parse the content of your body and insert vbcrlf ('hard returns') characters where needed. 解析您的身体内容,并在需要的地方插入vbcrlf (“硬性回报”)字符。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM