Send email using vba code

T

Thomas Kroljic

All,
about a month ago I posted here and received some excellent help/advice. I
would now like to enhance my app even further. I have a form that allow the
user to send an email out to a specific recipient. I am using the vba code
that was on Tony Toews (MVP) website.

What I would like to do now is add a graphic image to this outgoing email.
Not as an attachment, but as part of the email body. What I recently did was
create a template in Outlook and saved this file to a specific subdirectory.
My vba logic is listed below. If I comment out the .HTMLbody part, then I
receive the email using the template which contains the image. If I attempt
to use the .HTMLbody part in, then the template becomes an attachment to the
email instead.

Is there a way to use the template but add additional text to the body of
the email?

Thank you,
Thomas kroljic

Dim AppOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Dim MailAttachment As Outlook.Attachment

Set AppOutLook = CreateObject("Outlook.Application")
Dim xref As Hyperlink
' logon
Dim xnamemail As String
xnamemail = "Default"
Call AppOutLook.GetNamespace("MAPI").Logon(Profile:=xnamemail)
'Set MailOutLook = AppOutLook.CreateItemFromTemplate
("D:\StoreManagement\EmailTemplates\NewTest01.oft")

Dim xhtmlstring As String
For X = 1 To Len(Me.txt_body) (i have an Access form that the user
enters text into)
If mID(Me.txt_body, X, 1) = Chr(13) Then
xhtmlstring = xhtmlstring + "<br />"
Else
xhtmlstring = xhtmlstring + mID(Me.txt_body, X, 1)
End If
Next X

With MailOutLook
'Set body format to HTML
.BodyFormat = olFormatHTML
.To = Me.txt_to
.Subject = Me.txt_subject
If Me.txt_attachment <> "" And _
Not IsNull(Me.txt_attachment) Then
.Attachments.Add "d:\storemanagement\emailpdf\" & _
pEmailAttachment
End If

.HTMLBody = "<body>" & xhtmlstring & "<br />" & _
"<br />" & _
"<br />" & _
"<br />" & _
mEmpFullName & "<br />" & _
"<b>Jacobs Music Company<b/> " & "<br />" & _
"Phone Number: " & mPhoneNumber & "<br />" & _
IIf(mFaxNumber <> "", "Fax Number: " & mFaxNumber &
"<br />", "") & _
"Visit us at <a
href=http://www.jacobsmusic.com>www.jacobsmusic.com</a> " & "<br />" & _
"</body>"
'.Display True
.Send
End With
 
T

Tony Toews [MVP]

Thomas Kroljic said:
What I would like to do now is add a graphic image to this outgoing email.
Not as an attachment, but as part of the email body. What I recently did was
create a template in Outlook and saved this file to a specific subdirectory.
My vba logic is listed below. If I comment out the .HTMLbody part, then I
receive the email using the template which contains the image. If I attempt
to use the .HTMLbody part in, then the template becomes an attachment to the
email instead.

I would actually suggest asking in a Outlook VBA or programming
newsgroup. They'd be more familiar with the details of Outlook than
us Access folks. Also try visiting www.slipstick.com.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 
T

Thomas Kroljic

Thanks for your response.

I did some reading, Sue Mosher's book, and ended up using the following
steps to accomplish my goal:

1. I saved the Outlook Template that I created as a HTML text file. but
before I saved the file, I added something like %change this text% to the
bottom of the template.
2. my logic now opens (reads) the text file into a memory variable
3. Next I use the replace command and replace %change this text% to
whatever I need (in my case it becomes a sales persons name and info)
4. Once i complete the replace command, I then assign the memory variable
to the HTMLbody property of the mail item.

This works for me. Just thought I'd pass it on.

Thank you,
Thomas
 
T

Tony Toews [MVP]

Thomas Kroljic said:
I did some reading, Sue Mosher's book, and ended up using the following
steps to accomplish my goal:

1. I saved the Outlook Template that I created as a HTML text file. but
before I saved the file, I added something like %change this text% to the
bottom of the template.
2. my logic now opens (reads) the text file into a memory variable
3. Next I use the replace command and replace %change this text% to
whatever I need (in my case it becomes a sales persons name and info)
4. Once i complete the replace command, I then assign the memory variable
to the HTMLbody property of the mail item.

This works for me. Just thought I'd pass it on.

Excellent solution. Thanks for posting back.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top