我如何回复outlook邮件使用vba在excel与邮件线程?

92dk7w1h  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(185)

我正在寻找Excel中的VBA代码回复选定的邮件,但下面的代码创建单独的邮件,其中没有以前的消息在对话(线程)的身体。我在网上搜索,但其中大多数是旧代码,目前不工作。请帮助。

Sub Test_template()
    
    Dim emailApplication As Object
    Dim emailItem As Object
    
    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.ActiveExplorer.Selection.Item(1).ReplyAll
    
    emailItem.bcc = "XYZ.com"
    
    emailItem.Body = "Hi, have a nice day "
    
    emailItem.Display
    
    Set emailItem = Nothing
    Set emailApplication = Nothing
    
End Sub
vql8enpb

vql8enpb1#

这是你正在尝试的吗?我已经对代码进行了注解,并提供了相关的MSDN链接。如果你仍然卡住,那么只需询问。

Option Explicit

Sub Sample()
    Dim OutlookApp As Object
    Dim OutlookMail As Object

    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.ActiveExplorer.Selection.Item(1)
    
    '~~> Get MailItem.GetConversation method (Outlook)
    ' https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem.getconversation
    Dim OutlookConversation As Object
    Set OutlookConversation = OutlookMail.GetConversation

    '~~> Conversation.GetTable method (Outlook)
    ' https://learn.microsoft.com/en-us/office/vba/api/outlook.conversation.gettable
    Dim OutlookTable As Object
    Set OutlookTable = OutlookConversation.GetTable
    
    '~~> Obtains a 2D array from the Table.
    ' https://learn.microsoft.com/en-us/office/vba/api/outlook.table.getarray
    Dim OutlookAr As Variant
    OutlookAr = OutlookTable.GetArray(OutlookTable.GetRowCount)
    
    Dim OutlookReplyToThisMail As Object
    Set OutlookReplyToThisMail = OutlookMail.Session.GetItemFromID(OutlookAr(UBound(OutlookAr), 0))
    
    Dim MyMessage As String: MyMessage = "Hi, have a nice day "
    
    With OutlookReplyToThisMail.ReplyAll
        .BCC = "XYZ.com"
        .HTMLBody = MyMessage & .HTMLBody
        .Display
    End With
End Sub
7dl7o3gd

7dl7o3gd2#

.ReplyAll生成预期结果,但被emailItem.Body = "Hi, have a nice day "覆盖。

Option Explicit

Sub Test_template()
    
    Dim emailApplication As Object
    Dim emailItem As Object
    
    'Set emailApplication = CreateObject("Outlook.Application")
    
    ' mail has to be slelected in Outlook application so it has to be open already
    Set emailApplication = GetObject(, "Outlook.Application")
    Set emailItem = emailApplication.ActiveExplorer.Selection.Item(1).ReplyAll
    
    emailItem.BCC = "XYZ.com"
    
    'emailItem.Body = "Hi, have a nice day "
    
    emailItem.Body = "Hi, have a nice day " & emailItem.Body
    
    ' or
    'emailItem.htmlBody = "Hi, have a nice day " & emailItem.htmlBody
    
    emailItem.Display
    
    Set emailItem = Nothing
    Set emailApplication = Nothing
    
End Sub

相关问题