2016-02-12 314 views
1

我創建了一個子例程,它抓取所有相關的詳細信息和附件,爲我發送自動發送的電子郵件。這裏是我的代碼:使用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對象庫。

任何幫助,非常感謝!

回答

0

改變車身與內容:

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('硬返回')字符。

希望這會有所幫助。

+0

很抱歉的延遲反應不它仍然會把附件放在電子郵件的末尾,出於某種原因,我試着玩弄位置參數並試着#1-3,35-40,50和100.所有這些都通過附件在電子郵件的最後... – YASPLS

+0

@YASPLS您使用的是什麼版本的Outlook?這已經過Outlook2013的測試,並按照描述的方式運行。 – Cisco

+0

思科,感謝您的幫助。請參閱我的其他帖子。展望2 010,顯然這是一個沒有修復的已知錯誤。 – YASPLS

相關問題