excel 将email.HTMLBody连接到组收件箱回复

tf7tbtn2  于 2023-05-19  发布在  其他
关注(0)|答案(2)|浏览(183)

我有Excel VBA代码,可以在组收件箱子文件夹中搜索具有匹配主题的第一封电子邮件。然后它回答所有人。
我无法将我的文本添加到电子邮件的其余部分。
我看到过许多类似问题的响应都显示为.HTMLBody = "test" & .HTMLBody,但当调试到达这一行时,第二个.HTMLBody显示为
应用程序定义或对象定义错误

Sub Find_Email()

    Dim olMail As Outlook.MailItem
    Dim olReply As Outlook.MailItem
    Dim olItems As Outlook.Items
    Dim olNS As Namespace
    Dim olMailbox As Folder
    Dim olFolder As Folder
    Dim subFolder As Folder
    Dim BodyText As String

    Set olNS = GetNamespace("MAPI")
    Set olMailbox = olNS.Folders("Group_Inbox")
    Set olFolder = olMailbox.Folders("test_Folder")
    Set subFolder = olFolder.Folders("test_subFolder")
    Set olItems = subFolder.Items

    TheDate = Format(Date, "DD-MM-YYYY")
    TheDate1 = Format(Date, "YYYY-MM")
    TheDate2 = Format(Date, "YYYYMMDD")
    TheDate3 = Format(Date, "YYYY")

'Find most recent email and populate

    olItems.Sort "ReceivedTime", True
    For i = 1 To olItems.Count
        Set olMail = olItems(i)
        If InStr(olMail.Subject, "Desired_Subject " & TheDate) > 0 Then
            Set olReply = olMail.ReplyAll
            With olReply
                .Display
                .To = "Recipients@gmail.com"
                .CC = ""
                .Subject = "Test_Subject"
                '.Attachments.Add "Document_destination"
                .BodyFormat = olFormatHTML
                .HTMLBody = "This is a test email sending in Excel" & .HTMLBody ' This is the problem line.
                Exit Sub
            End With
        End If
    Next i
End Sub

我知道这可能是因为它在一个小组的收件箱里,这意味着它可能对你有用。

xggvc2p6

xggvc2p61#

试试这个(我不能测试它,只是一个想法)

'Somewehere declare this string variable
Dim incomingHTMLBody as string

olItems.Sort "ReceivedTime", True
For i = 1 To olItems.Count
    Set olMail = olItems(i)
    If InStr(olMail.Subject, "Desired_Subject " & TheDate) > 0 Then
        incomingHTMLBody = olMail.HTMLBody
        Set olReply = olMail.ReplyAll
        With olReply
            .Display
            .To = "Recipients@gmail.com"
            .CC = ""
            .Subject = "Test_Subject"
            '.Attachments.Add "Document_destination"
            .BodyFormat = olFormatHTML
            .HTMLBody = "This is a test email sending in Excel" & incomingHTMLBody
            Exit Sub
        End With
    End If
Next i
End Sub
qvk1mo1f

qvk1mo1f2#

在环境中引用Outlook对象时可能需要多加小心。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Sub Find_Email()

Dim objApp As Outlook.Application
Set objApp = CreateObject("outlook.application")

Dim objNS As Namespace
Set objNS = objApp.GetNamespace("MAPI")

Dim objMailbox As Outlook.Folder
Set objMailbox = objNS.Folders("Group_Inbox")

Dim objFolder As Outlook.Folder
Set objFolder = objMailbox.Folders("test_Folder")

Dim subFolder As Outlook.Folder
Set subFolder = objFolder.Folders("test_subFolder")

Dim objItems As Outlook.Items
Set objItems = subFolder.Items

Dim TheDate As Date
TheDate = Format(Date, "DD-MM-YYYY")

'Find most recent email and populate

objItems.Sort "ReceivedTime", True

Dim i As Long

Dim objMail As Outlook.MailItem     ' olMail is not a good variable name
Dim objReply As Outlook.MailItem

Debug.Print objItems.Count

For i = 1 To objItems.Count

    Debug.Print objItems(i).Subject
    
    If objItems(i).Class = olMail Then   ' verify item is a mailitem
    
        Set objMail = objItems(i)
        
        If InStr(objMail.Subject, "Desired_Subject " & TheDate) > 0 Then

            Set objReply = objMail.ReplyAll
            With objReply
                .Display
                .To = "Recipients@gmail.com"
                .CC = ""
                .Subject = "Test_Subject"
                '.Attachments.Add "Document_destination"
                .BodyFormat = olFormatHTML
                
                Debug.Print .htmlbody   ' verify property is available
                
                .htmlbody = "This is a test email sending in Excel" & .htmlbody ' This is the problem line.
                
                Exit For
            End With
        End If
        
    End If
    
Next i

End Sub

相关问题