簡體   English   中英

發送時電子郵件正文丟失(outlook vba)

[英]E-Mail body is lost when sending (outlook vba)

我正在嘗試編寫一個宏,該宏在發送原始電子郵件之前將自動通知發送到特定地址。 (就像一個抄送,而沒有實際使用抄送。)

原始格式的電子郵件(包括文本,表格和圖片)的內容應復制並粘貼到新電子郵件中,然后自動發送。 當我僅顯示消息時,一切正常,但是在實際發送電子郵件時,一切都沒有。

這是我的代碼:

Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem
Dim BodyText As Object

' Create the message.
Set objMsg = Application.CreateItem(olMailItem)

'copy body of current item
Set activeMailMessage = ActiveInspector.CurrentItem
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy

'paste body into new email
Set BodyText = objMsg.GetInspector.WordEditor.Range
BodyText.Paste

'set up and send notification email
With objMsg
    .To = "test@domain.com"
    .Subject = "text" 
    .Send
End With

文本應該像這樣粘貼到正文中,但不會粘貼:

With objMsg
    .To = "test@domain.com"
    .Subject = "test" 
    .body = bodytext.paste 
    .Send
End With

當我使用.display將顯示正確的內容。 但是,當我直接發送它(不首先使用.display )時,所有信息都會丟失,並且會發送空電子郵件。 我能做什么?

我可以在原始電子郵件中添加密件抄送以達到相同的結果,但是原始電子郵件並不總是發送,而此通知應該發送。

嘗試在調用Paste方法之后調用Save方法。

您的代碼實際上從未在objMsg對象中設置電子郵件的正文。 當您顯示objMsg時它正在工作,因為您與“檢查器”進行了交互。

如果直接設置HTMLBody(如果要保留格式)或objMsg上的Body屬性,則它將按以下示例所示工作。

With objMsg
    .HTMLBody = activeMailMessage.HTMLBody
    .To = "test@domain.com"
    .Subject = "text" 
    .Send
End With

鮑勃(Bob),關於您對電子郵件中嵌入的圖像的問題因上述方法而丟失。 一種替代解決方案是使用MailItem的Copy方法來創建與原始Item完全相同的新MailItem。 這還將保留發送給誰的電子郵件,您需要清除此電子郵件以確保只有預定的收件人會收到它。

Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem

' Create the new message.
Set objMsg = Application.CreateItem(olMailItem)

' Assign the current item to activeMailMessage
Set activeMailMessage = ActiveInspector.CurrentItem

' Copy the current item to create a new message
Set objMsg = activeMailMessage.Copy

' Clear any existing recipients of the e-mail, as these will be retained in the Copy
While objMsg.Recipients.Count > 0
    objMsg.Recipients.Remove 1
Wend

'set up and send notification email
With objMsg
    .To = "test@domain.com"
    .Subject = "text" 
    .Send
End With

這將保留原始電子郵件中的圖像和其他附件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM