VBA My Email body doesn t concatenate with itself application-defined or object-defined error

0 votes

My script automatically responds to the first email with a subject that matches after looking through a group inbox subfolder. Then it replies to everyone. I am unable to append my text to the remainder of the email after I have filled it in. only either or. I've observed numerous solutions to issues of a similar nature. HTMLBody is equal to "test". When the debug hits this line, the second option is HTMLBody. "Application-defined or object-defined error" is displayed for HTMLBody. Any suggestions as to what the issue might be or where I might find the information from the chain of emails I need to submit would be highly appreciated.

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
Feb 16, 2023 in Others by Kithuzzz
• 38,000 points
928 views

1 answer to this question.

0 votes

Try this:

'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
answered Feb 16, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Wordpress Critical Error Due to Plugin Conflict with my Functions.php

// WooCommerce Stock message add_filter( 'woocommerce_get_availability', 'mw_get_availability', ...READ MORE

answered Feb 26, 2022 in Others by narikkadan
• 63,600 points
1,127 views
0 votes
1 answer
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,260 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,697 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
977 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,090 views
0 votes
1 answer

VBA Excel FormulaR1C1 "Application Defined or Object Defined Error"

Consider using a fixed template for the ...READ MORE

answered Mar 20, 2023 in Others by narikkadan
• 63,600 points
462 views
0 votes
1 answer

Wordpress Critical Error Due to Plugin Conflict with my Functions.php

modify your code like this and will ...READ MORE

answered Feb 24, 2022 in Others by narikkadan
• 63,600 points
1,002 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP