Forum Discussion

Leozin28's avatar
Leozin28
Copper Contributor
Jul 22, 2024

Followup sheet - Macro

Hello everyone,

After days of failing, I have come here to ask for your help gurus please!!

 

I am working on a VBA project in Excel that involves three main tabs:

  1. Mailing: This tab contains a table with client names and emails. I have a button that allows sending emails to all clients listed in the table. This part is working perfectly!!!.

  2. Followup: In this tab, I want to retrieve the 100 most recent emails from my inbox and fill them into a table. The goal is to read these emails and compare them with the emails sent from the "Mailing" tab to check if I have received responses from my clients.

  3. Inbox: This tab serves as a database where the 100 most recent emails are stored. This is working as well!!

What I need:

  • WORKING - Retrieve the 100 most recent emails: I need help writing a VBA code that retrieves the 100 most recent emails from my Outlook inbox and fills the "Inbox Emails" tab.
  • NOT WORKING - Compare and update status: After filling the "Inbox Emails" tab, I need the VBA to read each of the 100 emails and compare them with the emails sent listed in the "Mailing" tab. If there is a response from a client, the status should be updated to "New Answer". If there is no response, the status should be "Waiting".

I appreciate any help in advance and am available to clarify any questions. Please reach to me in any platform!

 

2 Replies

  • JKPieterse's avatar
    JKPieterse
    Silver Contributor
    How might your code detect whether an email has had a reply? I would think a combination of the subject line and the sender address against the subject line and To address of the original sent email? Could be done using a simple XLOOKUP formula instead if VBA.
    • Leozin28's avatar
      Leozin28
      Copper Contributor

      Hey JKPieterse, ty for your help!

       

      In fact, I need a VBA macro cause with those actual replies, it would fill another column with "REPLIED / NOT REPLIED" and this would lead to another VBA code (inside the same button), for sending a followup e-mail. Kind of joining macros here to make as practical as I could.

       

      After hours of trial and error, I got into somewhere that worked like that VLOOKUP you mentioned, but using VBA. I´ll add here in this message in case you want to see or if it might be useful for you.

       

      Just for emphasizing, I had 3 tabs, with the first called Mailing, with the second called Follow1 and, at last, Follow2.

       

      In case there is any doubt, feel free to contact me and I will gladly help! 🙂

       

      Sub SendResponses()
      Dim wsFollowup As Worksheet
      Dim wsFollowup2 As Worksheet
      Dim wsMailing As Worksheet
      Dim name As String
      Dim email As String
      Dim subject As String
      Dim status As String
      Dim objOutlook As Object
      Dim objEmail As Object
      Dim i As Long
      Dim emailTitle As String
      Dim emailBody1 As String
      Dim emailBody2 As String
      Dim signature As String
      Dim phone As String
      Dim assessorEmail As String
      Dim oldEmail As String
      Dim oldTitle As String
      Dim body1 As String
      Dim body2 As String

      ' Define the Followup worksheet
      Set wsFollowup = ThisWorkbook.Sheets("Followup 1")
      Set wsMailing = ThisWorkbook.Sheets("Mailing")
      Set wsFollowup2 = ThisWorkbook.Sheets("Followup 2")

      ' Initialize Outlook
      Set objOutlook = getoutlook()

      ' Email title
      emailTitle = wsFollowup.Range("K2").Value

      ' Email body part 1
      emailBody1 = wsFollowup.Range("K3").Value

      ' Email body part 2
      emailBody2 = wsFollowup.Range("K4").Value

      ' Signature
      signature = wsFollowup.Range("K7").Value

      ' Phone
      phone = wsFollowup.Range("K8").Value
      wsFollowup.Range("K8").NumberFormat = "(##) #####-####"

      ' Old email
      oldTitle = wsMailing.Range("k2").Value
      body1 = wsMailing.Range("K3").Value
      body2 = wsMailing.Range("k4").Value

      ' Assessor email
      assessorEmail = wsFollowup.Range("K9").Value

      ' Iterate over each row in the Followup worksheet
      For i = 2 To wsFollowup.Cells(wsFollowup.Rows.Count, "A").End(xlUp).Row
      email = wsFollowup.Cells(i, 3).Value ' Column C of the Followup sheet (Email)
      status = wsFollowup.Cells(i, 6).Value

      ' Check if the cell in column C contains an email and the status is "AWAITING"
      If Len(email) > 0 And (status = "AWAITING" Or status = "REPLIED!") Then
      name = wsFollowup.Cells(i, 1).Value ' Name in column A
      nickname = wsFollowup.Cells(i, 2).Value ' Subject in column B

      ' Create email item for follow-up
      Set objEmail = objOutlook.CreateItem(0) ' 0 represents olMailItem
      If Not objEmail Is Nothing Then
      With objEmail
      .Display
      .Sender = assessorEmail
      .Subject = nickname & emailTitle
      .Body = name & ", how are you?" & vbCrLf & vbCrLf & _
      emailBody1 & vbCrLf & vbCrLf & _
      emailBody2 & vbCrLf & vbCrLf & _
      "Sincerely," & vbCrLf & _
      signature & vbCrLf & _
      phone & vbCrLf & _
      assessorEmail & vbCrLf & _
      "URL link" & vbCrLf & _
      "Address" & vbCrLf & _
      "São Paulo, SP" & vbCrLf & vbCrLf & _
      " " & _
      "___________________________________________________________________________________________________________________________________________________________________________________________________________" & vbCrLf & vbCrLf & _
      "Subject: " & nickname & oldTitle & vbCrLf & vbCrLf & _
      name & ", how are you?" & vbCrLf & _
      body1 & vbCrLf & vbCrLf & _
      "Sincerely," & vbCrLf
      .To = email
      '.Send
      End With
      Else
      MsgBox "Unable to create email item.", vbCritical
      End If

      ' Update the status in column F of the Followup sheet to "REPLIED!"
      wsFollowup.Cells(i, 6).Value = "REPLIED!"
      wsFollowup2.Cells(i, 6).Value = "REPLIED!"
      End If
      Next i

      End Sub

       

Resources