Inserting Text into an Outlook Template

sbrown106

Member
Local time
Today, 15:08
Joined
Feb 6, 2021
Messages
77
Hello everybody, Ive been struggling with this for the last day or so. I've tried 2 ways. I am trying to insert text (names/address etc) into the body of an email (with terms and conditions etc) that has been loaded as a template in MS Access.
The problem is I need a check box in the email body to select to show that the person reading the email has understood the terms and conditions. I thought it would be easier to create this template in outlook (with the check box) and then load it into Access and then insert the name/address etc into the body of the email - but this then just overwrites the text in the template. Ive tried it the other way and included the names/address etc in the vba code - but then cant figure out how to put a check box in vba. Ive included the code below (which works with the check box/template - but I cant figure out how to include the names in the body).
Or if its easier to include a check box in vba with the body of the email in the code (which I also got to work) - but as I say i couldnt do that bit with the checkbox

I hope that makes sense, somebody may have better suggestions - code for loaded template with check box without names/address below
Code:
Private Sub cmdesign_Click()
Dim rs As DAO.Recordset
Dim olApp As Outlook.Application
Dim myitem As Outlook.MailItem
Dim olNS As Outlook.NameSpace
Dim email As String
Dim fullname As String
Dim clientID As String
Dim incidentdate As String
Dim message As String
Dim SQL As String
 
Set rs = CurrentDb.OpenRecordset( _
    "Select A.fldClientID, A.fldIncidentDate, A.fldDateCreated,B.fldEmailAddress, B.fldFullName FROM tbl_Instructions AS A INNER JOIN tbl_ClientDetails AS B ON A.fldClientID=B.fldClientID " & _
    "WHERE A.fldClientID = " & Me!txtClientID & "", dbOpenSnapshot, dbReadOnly)
'DoCmd.RunSQL SQL
    Do Until rs.EOF
        ' put required fields check in here
        If Me!txtClientID = rs![fldClientID] Then
        incidentdate = rs![fldIncidentDate]
        fullname = rs![fldFullName]
        clientID = rs![fldClientID]
        email = rs![fldEmailAddress]
        End If
    rs.MoveNext
    Loop
rs.Close
Set rs = Nothing

Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")


Set myitem = olApp.CreateItemFromTemplate("C:\Users\steph\AppData\Roaming\Microsoft\Templates\CompanyFOAR.oft")

With myitem
    .To = email
    .Subject = "Company" & "-" & " " & fullname
'    .Body = message
    .Display
End With

Set olApp = Nothing

End Sub
 
Hi. Just curious, can you post a copy of the template with the checkbox?
 
One way would be to create the template with placeholders and then replace those with the relevant values.?
 
I'm getting an error with modifying the HTMLBody property, and I can't do any research right now due to firewall restrictions. I'll try again later...
 
Just cheated and copied and pasted from a word doc
Okay, I was wondering if we could bypass using a template and simply build the email from within Access.

You could give it a try. Copy the text from Word and paste it into a Rich Text Long Text field in a table and see if you can use that to create the email.
 
Hi Gasman, how would I do that in outlook? Is there a simple example, sorry I’m new to this. I’ve recreated the email in access in vba with names/addresses the problem I then have is trying to recreate the text box for the recipient to agree. If I do it the other way so I have a template with a text box I can’t then get the names / addresses in .Unless there is a way to insert the names/addresses etc above the body text in the email templates. I hope that makes sense
 
My thoughts were to use the Replace,() function in the email body?
You would know what to look for, and you know what to replace.?
 
Okay, I was wondering if we could bypass using a template and simply build the email from within Access.

You could give it a try. Copy the text from Word and paste it into a Rich Text Long Text field in a table and see if you can use that to create the email.
I’ve recreated the email in access previously with names / addressesbut couldn’t get a checkbox for recipient verification on confirming they understand terms/conditions - I will try the rich text long field for the check box- thanks. If that doesn’t work will see if there is a different approach to this, thanks again
My thoughts were to use the Replace,() function in the email body?
You would know what to look for, and you know what to replace.?
Thanks Gasman - thats worked, its not very elegant because my coding could be better - Ive just put multiple .body Replace statements in to replace text like 'hereby' to strname & 'hereby' - so thanks for that!
 
I am all for tidy code :) but was thinking you would just have

InsertNameHere hereby agree....
And you would just replace InsertNameHere with Steve Brown, etc
and replace each place marker in turn?
 
I'm getting an error with modifying the HTMLBody property, and I can't do any research right now due to firewall restrictions. I'll try again later...
Hi. Sorry for the delay... I don't get the same error when trying to modify the HTMLBody from my home machine. So, I guess it wasn't a problem with the template after all.

Glad to hear you got it sorted out. Good luck with your project.
 
I am all for tidy code :) but was thinking you would just have

InsertNameHere hereby agree....
And you would just replace InsertNameHere with Steve Brown, etc
and replace each place marker in turn?
Thanks I’ve changed it to what you suggested - it makes more sense!
 

Users who are viewing this thread

Back
Top Bottom