【问题标题】:Excel VBA to include body in the forwarded outlook emailExcel VBA 在转发的 Outlook 电子邮件中包含正文
【发布时间】:2017-03-15 04:45:14
【问题描述】:

我正在尝试通过循环转发基于 A 列中提供的主题的电子邮件。它工作得很好,但我还想将 C 列中的内容包含到每个相应的邮件中。

同时从初始邮件中删除发件人和收件人详细信息。

请求模板:

正文内容也应该使用下面提到的列值。

有人可以帮我删除这些详细信息并将其包含在下面..

Option Explicit
Public Sub Example()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim Inbox As Outlook.MAPIFolder
Dim Item As Variant
Dim MsgFwd As MailItem
Dim Items As Outlook.Items
Dim Email As String
Dim Email1 As String
Dim ItemSubject As String
Dim lngCount As Long
Dim i As Long
Dim RecipTo As Recipient
Dim RecipCC As Recipient
Dim RecipBCC As Recipient
Dim onbehalf As Variant



Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Set Items = Inbox.Items

i = 2 '  i = Row 2

With Worksheets("Sheet1") ' Sheet Name
    Do Until IsEmpty(.Cells(i, 1))

    ItemSubject = .Cells(i, 1).Value '(i, 1) = (Row 2,Column 1)
    Email = .Cells(i, 16).Value '(i, 2) = (Row 2,Column 2)
    Email1 = .Cells(i, 2).Value

        '// Loop through Inbox Items backwards
        For lngCount = Items.Count To 1 Step -1
            Set Item = Items.Item(lngCount)

            If Item.Subject = ItemSubject Then ' if Subject found then
                Set MsgFwd = Item.Forward
                Set RecipTo = MsgFwd.Recipients.Add(Email1) ' add To Recipient
                Set RecipTo = MsgFwd.Recipients.Add("sen@aa.com")
                Set RecipBCC = MsgFwd.Recipients.Add(Email) ' add BCC Recipient
                MsgFwd.SentOnBehalfOfName = "doc@aa.com"


                    RecipTo.Type = olTo
                    RecipBCC.Type = olBCC
                    MsgFwd.Display

            End If
        Next ' exit loop

        i = i + 1 '  = Row 2 + 1 = Row 3
    Loop
End With

Set olApp = Nothing
Set olNs = Nothing
Set Inbox = Nothing
Set Item = Nothing
Set MsgFwd = Nothing
Set Items = Nothing

MsgBox "Mail sent"
End Sub

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    将新变量添加为字符串Dim EmailBody As String,然后在您的Do Loop

    中分配给C列EmailBody = .Cells(i, 3).Value

    要从 Item.Forward 正文中删除以下内容,只需将您的 Item.Body 添加到您的 MsgFwd.Body - 它应该替换仅包含Item.Body 的整个转发电子邮件正文


    MsgFwd.HTMLBody = EmailBody & "<BR>" & "<BR>" & Item.HTMLBody


    例子

    Dim EmailBody As String
    With Worksheets("Sheet1") ' Sheet Name
        Do Until IsEmpty(.Cells(i, 1))
    
        ItemSubject = .Cells(i, 1).Value '(i, 1) = (Row 2,Column 1)
        Email = .Cells(i, 16).Value '(i, 2) = (Row 2,Column 2)
        Email1 = .Cells(i, 2).Value
        EmailBody = .Cells(i, 3).Value
    
            '// Loop through Inbox Items backwards
            For lngCount = Items.Count To 1 Step -1
                Set Item = Items.Item(lngCount)
    
                If Item.Subject = ItemSubject Then ' if Subject found then
                    Set MsgFwd = Item.Forward
                    Set RecipTo = MsgFwd.Recipients.Add(Email1) ' add To Recipient
                    Set RecipTo = MsgFwd.Recipients.Add("sen@aa.com")
                    Set RecipBCC = MsgFwd.Recipients.Add(Email) ' add BCC Recipient
                    MsgFwd.SentOnBehalfOfName = "doc@aa.com"
    
                    RecipTo.Type = olTo
                    RecipBCC.Type = olBCC
    
                    Debug.Print Item.Body ' Immediate Window
    
                    MsgFwd.HTMLBody = EmailBody & "<BR>" & "<BR>" & Item.HTMLBody
                    MsgFwd.Display
    
                End If
            Next ' exit loop
    

    【讨论】:

    • 感谢@0m3r 完美运行.. 只缺少一件事。在正文内容中,Hello“D”指的是 D column.value。所以我需要根据值自动更新名称。有可能吗?
    • @Kelvin 只需在您的do loop 示例BodyName = .Cells(i, 4).Value 中添加另一个变量,然后在MsgFwd.HTMLBody = EmailBody &amp; " " &amp; BodyName &amp; "&lt;BR&gt;" &amp; "&lt;BR&gt;" &amp; Item.HTMLBody 上添加一个变量
    • 太棒了,非常感谢..我真的欠你:)
    猜你喜欢
    • 2020-10-06
    • 2010-11-20
    • 2015-06-27
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多