0% found this document useful (0 votes)
10 views2 pages

Future Replace Placeholders

This VBA macro replaces placeholders in an email draft with actual values. It searches an email body for date and recipient placeholders and replaces them. It gets the replacement values from an Excel file, calculating a future date and looking up the email address associated with a recipient's full name. It applies bold formatting to the inserted date and updates the email with the modified body.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Future Replace Placeholders

This VBA macro replaces placeholders in an email draft with actual values. It searches an email body for date and recipient placeholders and replaces them. It gets the replacement values from an Excel file, calculating a future date and looking up the email address associated with a recipient's full name. It applies bold formatting to the inserted date and updates the email with the modified body.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sub ReplacePlaceholders()

Dim olInspector As Outlook.Inspector


Dim olMail As Outlook.MailItem
Dim EmailText As String
Dim NewDate As Date
Dim PlaceholderDate As String
Dim PlaceholderEmail As String
Dim StartPosDate As Long
Dim StartPosEmail As Long
Dim EndPosDate As Long

' Set the placeholders to search for


PlaceholderDate = "DVfuture.date"
PlaceholderEmail = "DVfullname.email"

' Get a reference to the currently open mail item


Set olInspector = Application.ActiveInspector

' Check if there's a valid inspector


If Not olInspector Is Nothing Then
' Check if the inspector's current item is a mail item
If TypeOf olInspector.CurrentItem Is Outlook.MailItem Then
Set olMail = olInspector.CurrentItem
EmailText = olMail.HTMLBody

' Check if the date placeholder exists in the email body


StartPosDate = InStr(EmailText, PlaceholderDate)
If StartPosDate > 0 Then
' Calculate the future date (36 hours from now)
NewDate = Now + TimeSerial(36, 0, 0) ' 36 hours

' Format the future date as "Month Day, Year" (e.g., "October 23,
2023")
NewDateFormatted = Format(NewDate, "MMMM dd, yyyy")

' Replace the date placeholder with the formatted date


EmailText = Replace(EmailText, PlaceholderDate, NewDateFormatted)

' Apply bold formatting to the inserted date using HTML tags
EmailText = Replace(EmailText, NewDateFormatted, "<b>" &
NewDateFormatted & "</b>")

' Update the HTMLBody with the modified content


olMail.HTMLBody = EmailText
End If

' Check if the email placeholder exists in the email body


StartPosEmail = InStr(EmailText, PlaceholderEmail)
If StartPosEmail > 0 Then
' Extract the fullname from the email body
EndPosDate = InStr(StartPosEmail, EmailText, ".") - 1
Dim FullName As String
FullName = Mid(EmailText, StartPosEmail + Len(PlaceholderEmail),
EndPosDate - StartPosEmail - Len(PlaceholderEmail))

' Get the email address based on the fullname


Dim Email As String
Email = GetEmailFromExcel(FullName) ' Replace with your actual
lookup function
' Replace the email placeholder with the actual email
EmailText = Replace(EmailText, PlaceholderEmail & FullName, Email)

' Update the HTMLBody with the modified content


olMail.HTMLBody = EmailText
End If
End If
End If
End Sub

Function GetEmailFromExcel(FullName As String) As String


Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object
Dim LastRow As Long
Dim i As Long

' Set the path to your Excel file


Dim ExcelFilePath As String
ExcelFilePath = "C:\Path\To\Your\Excel\File.xlsx" ' Replace with your actual
file path

Set xlApp = CreateObject("Excel.Application")


Set xlWB = xlApp.Workbooks.Open(ExcelFilePath)
Set xlWS = xlWB.Sheets(1) ' Assuming the data is in the first sheet

' Find the email associated with the Full Name


LastRow = xlWS.Cells(xlWS.Rows.Count, "A").End(-4162).Row ' Find the last row
in column A
For i = 2 To LastRow ' Assuming data starts from row 2 (headers in row 1)
If xlWS.Cells(i, 1).Value = FullName Then ' Assuming Full Name is in column
A
GetEmailFromExcel = xlWS.Cells(i, 2).Value ' Assuming email is in
column B
Exit For
End If
Next i

' Close Excel without saving changes


xlWB.Close False
xlApp.Quit
Set xlWS = Nothing
Set xlWB = Nothing
Set xlApp = Nothing
End Function

You might also like