CDO Using a text file template (1 Viewer)

mousemat

Completely Self Taught
Local time
Today, 15:47
Joined
Nov 25, 2002
Messages
233
Hi all.

I have CDO working well to send a test message to the recipients of a table (This will change later to an on demand query)

What I'd like to do is use a textfile template for the message which will be populated from the recordset.

What do I need to do with the code to make this work and is there anything special I need to do with the textfile in terms of placing the requisite data from the recordset?
Code:
Option Compare Database

Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
' Use basic (clear-text) authentication.
Const cdoBasic = 1
' Use NTLM authentication
Const cdoNTLM = 2 'NTLM

Public Sub SendEmail()
    Dim rs As DAO.Recordset

    Dim imsg As Object
    Dim iconf As Object
    Dim flds As Object
    Dim schema As String

    Set imsg = CreateObject("CDO.Message")
    Set iconf = CreateObject("CDO.Configuration")
    Set flds = iconf.Fields

    ' send one copy with SMTP server (with autentication)
    schema = "http://schemas.microsoft.com/cdo/configuration/"
    flds.Item(schema & "sendusing") = cdoSendUsingPort
    flds.Item(schema & "smtpserver") = "serverdetails.co.uk"
    flds.Item(schema & "smtpserverport") = 25
    flds.Item(schema & "smtpauthenticate") = cdoBasic
    flds.Item(schema & "sendusername") = "me@home.com"
    flds.Item(schema & "sendpassword") = "12345780"
    flds.Item(schema & "smtpusessl") = False
    flds.Update

Set rs = CurrentDb.OpenRecordset("SELECT * FROM Customers") 'This is where the records come from, most likely a query run on demand

If Not (rs.EOF And rs.BOF) Then
    Do Until rs.EOF = True
        'On Error Resume Next
        With imsg
            .To = rs.Fields("Email")
            .From = """SENDER"" <XXX@somewhere.com>"
            .Subject = rs.Fields("Event")
            .TextBody = "Test Message"
            Set .Configuration = iconf
            .Send
        End With
        
        rs.MoveNext
    Loop
    
    Set iconf = Nothing
    Set imsg = Nothing
    Set flds = Nothing
    
End If

MsgBox "Finished looping through records, all emails have been sent."
rs.Close 'Close the recordset

End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:47
Joined
Oct 29, 2018
Messages
21,449
Hi. Are you asking how to read a text file using code?
 

mousemat

Completely Self Taught
Local time
Today, 15:47
Joined
Nov 25, 2002
Messages
233
Basically yes, I want to load the textfile which has the body of the email message and also populate various parts of the message to make it personal to the reader
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:47
Joined
Oct 29, 2018
Messages
21,449
Basically yes, I want to load the textfile which has the body of the email message and also populate various parts of the message to make it personal to the reader
If so, you'll need placeholder text in your template to replace them with the data.
 

mousemat

Completely Self Taught
Local time
Today, 15:47
Joined
Nov 25, 2002
Messages
233
I'm presuming thats something like .fieldname

How would I load it via the code given
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:47
Joined
Oct 29, 2018
Messages
21,449
I'm presuming thats something like .fieldname

How would I load it via the code given
Hi. Check out this article.

As for the placeholder text, maybe something like "<<customer name>>."
 

mousemat

Completely Self Taught
Local time
Today, 15:47
Joined
Nov 25, 2002
Messages
233
Thanks for that, I'll have a play around and see if I can get to working
 

mousemat

Completely Self Taught
Local time
Today, 15:47
Joined
Nov 25, 2002
Messages
233
Got it to send the text file. now just trying to sort the laceholders.

"<<customer name>>." just gives me "<<customer name>>."
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:47
Joined
Oct 29, 2018
Messages
21,449
Got it to send the text file. now just trying to sort the laceholders.

"<<customer name>>." just gives me "<<customer name>>."
Hi. The placeholders are there, so you can replace them with data. For example:
Code:
.Body = Replace(strBody,"<<customer name>>",rs!CustName)
 

mousemat

Completely Self Taught
Local time
Today, 15:47
Joined
Nov 25, 2002
Messages
233
Hmm getting there slowly.

Ive added the Placeholders in the code, on separate lines and added the place holders to the text file but only getting the first placeholder populated.
Code:
If Not (rs.EOF And rs.BOF) Then
    Do Until rs.EOF = True
        'On Error Resume Next
        With imsg
            .To = rs.Fields("Email")
            .From = """SENDER"" <XXX@somewhere.com>"
            .Subject = rs.Fields("Event")
            .TextBody = BodyText
            .TextBody = Replace(BodyText, "<<Customer>>", rs!Customer)
            .TextBody = Replace(BodyText, "<<Event>>", rs!Event)
            .TextBody = Replace(BodyText, "<<Name>>", rs!Name)
            TextBody = Replace(BodyText, "<<Email>>", rs!Email)
            Set .Configuration = iconf
            .Send
        End With

Ive
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:47
Joined
Oct 29, 2018
Messages
21,449
Hmm getting there slowly.

Ive added the Placeholders in the code, on separate lines and added the place holders to the text file but only getting the first placeholder populated.
Code:
If Not (rs.EOF And rs.BOF) Then
    Do Until rs.EOF = True
        'On Error Resume Next
        With imsg
            .To = rs.Fields("Email")
            .From = """SENDER"" <XXX@somewhere.com>"
            .Subject = rs.Fields("Event")
            .TextBody = BodyText
            .TextBody = Replace(BodyText, "<<Customer>>", rs!Customer)
            .TextBody = Replace(BodyText, "<<Event>>", rs!Event)
            .TextBody = Replace(BodyText, "<<Name>>", rs!Name)
            TextBody = Replace(BodyText, "<<Email>>", rs!Email)
            Set .Configuration = iconf
            .Send
        End With

Ive
Yeah, instead of BodyText, use .TextBody in the Replace() lines.
 

Isaac

Lifelong Learner
Local time
Today, 07:47
Joined
Mar 14, 2017
Messages
8,774
Try changing
these
.TextBody = Replace(BodyText, "<<Customer>>", rs!Customer)

to

.TextBody = Replace(.TextBody, "<<Customer>>", rs!Customer)
 

mousemat

Completely Self Taught
Local time
Today, 15:47
Joined
Nov 25, 2002
Messages
233
Excellent. That's great. Many thanks for your help.
 

Users who are viewing this thread

Top Bottom