Insert Pdf/jpeg/etc. into body of email. (1 Viewer)

Eljefegeneo

Still trying to learn
Local time
Today, 14:39
Joined
Jan 10, 2011
Messages
904
I have a form that allows me to type in a Subject line, attach a file, write a short note and it is sent out to the list of clients via email. But I want to do away with the attachment; rather have it in the body of the email. Presently my code (which works quite well and was worked out on a previous post – thank you) is:
Code:
  Dim Subj, Bdy, Attch, Eml, EmlCc As String
  Dim db As DAO.Database
  Dim rs As DAO.Recordset
  Set db = CurrentDb()
  Set rs = db.OpenRecordset("tblEmailBlast")
        With rs
                rs.MoveFirst
                
                Do While Not .EOF
  '---Emails the schedule
  Dim olApp As New Outlook.Application
  Dim mItem As Outlook.mailitem  ' An Outlook Mail item
  Set olApp = CreateObject("Outlook.Application")
  Set mItem = olApp.CreateItem(olMailItem)
  Eml = rs("Email")
  EmlCc = Nz(rs("Email2"), "")
  With mItem
      .To = Eml
      .CC = EmlCc
      .Subject = Subj
      .Body = Format(Date, "Long Date") & MsgX & "Dear" & " " & rs("Fname") & " " & rs("Surname") & ":" & MsgX & Bdy & MsgX & MsgZ1
      .Attachments.Add (Attch)
      .Display  '(Only for test purposes)
      '.Send  '(Only when thoroughly tested)
  End With                  
                      .MoveNext
                Loop
        End With
     
        rs.Close
        Set mItem = Nothing
       ' SetOlApp = Nothing
        End If
  Exit Sub
I have done extensive research looking up ways to do this, but all posts on the internet are either too confusing to me or don’t seem to work (yes, I know "doesn't work is very vague). So what I am asking at this time is a nudge in the right direction. For example, should my ".Body" be a pdf, picture, ?, etc. And where might I find some code that would give me a start. Or is it even possible with Access vba?
 

Minty

AWF VIP
Local time
Today, 22:39
Joined
Jul 26, 2013
Messages
10,367
Access is not the restriction here, Outlook is. You are creating an Outlook email with this code via Access. So you need to ask if what you want is possible in Outlook.

My understanding is that images can be inserted, as part of an html body. PDF's can not directly be viewed or displayed directly in an outlook email to the best of my knowledge, but I'm happy to be proved wrong.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:39
Joined
Feb 28, 2001
Messages
27,128
Are you saying that you want the body of the referenced document to take the place of the body of the e-mail? If that is the case, you cannot do that.

It isn't Access OR Outlook that is doing this to you. It is the RFC (TCP/IP protocol definition) for SMTP-based messaging that is stopping you. See, for example,

https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol

Look at the "Protocol Overview" to see the details. Here is the summary:

Mail transmission is defined as a serial character-oriented method of sending messages from point A to point B in a "lowest-common-denominator" method. I.e. send it so even a stupid machine can understand it. The standard stems from a time when network connections included phone modems and limited encoding options.

The body of an e-mail is sent using a protocol that will not allow you to send binary-encoded data directly, and most other documents from Word, Acrobat, Excel, etc. are binary-encoded because they contain things like embedded images, special formatting characters, etc. Those documents must be MIME-encapsulated to be added as attachments. MIME encapsulation is like sending a big envelope with a bunch of smaller envelopes in it.

True, you CAN send HTML-based message bodies because they involve methods of using Hyper-Text Markup Language (HTML...) to include formatting information along with the actual text. But HTML is itself only made up of text and the difference is in how that text is interpreted on the other end of that message path. If the other end won't allow HTML, then you are absolutely stuck no matter WHAT you send.

You COULD reference an "external image" as part of an HTML body IF your message receiver allowed such a thing, but you would have to send along the encapsulated image as part of the message. I'll also tell you that if you tried to do that in a message to any U.S. Dept. of Defense site, your message would be blocked by the anti-virus scanner associated with your mail server because that kind of HTML encapsulation is EXACTLY how viruses get delivered.

My advice is to rethink what you are trying to do with the understanding that you are trying to exceed the basic standards of mail transmission. And the reason I know this is because for the last 28+ years before I retired, I was a system administrator, system analyst, and security co-manager for a major U.S. Navy Reserve personnel system. I had to know networking protocols and security principles inside and out.
 

Eljefegeneo

Still trying to learn
Local time
Today, 14:39
Joined
Jan 10, 2011
Messages
904
In my research, it is possible to do it in Outlook alone, so I thought it might be possible via vba through Access. One of the numerous sites that show how this is done is:

http://smallbusiness.chron.com/display-pdf-email-47272.html

With this method you can insert a pdf, jpeg, etc. into the body of the Outlook email. I have tried it with (some) success, but this method is not a good solution as I have to go through the record set to get the individual emails, names, etc.

Perhaps there is a third party system that would allow me to do this?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:39
Joined
Feb 28, 2001
Messages
27,128
Not aware of one, but then my last job was with the U.S. Dept. of Defense and they tended to like homogeneous systems - all Microsoft utilities where possible - so I never had a chance to try anything else.

The article essentially "cheats" by changing the other document format to a JPEG image, which as previously noted, could be displayed via HTML if you included the .JPG file as an attachment. Technically, you cannot DIRECTLY make an e-mail message anything other than text or HTML (or XML, which is also text-oriented).
 

Eljefegeneo

Still trying to learn
Local time
Today, 14:39
Joined
Jan 10, 2011
Messages
904
This code works, but need to figure out how to insert the Date, two carriage returns, Dear Mr. Jones, etc. parse the Attachment path so only the name.jpeg shows, etc. I have never worked with HTML before. Any help would be appreciated.

Code:
  Private Sub Command79_Click()
  Dim olApp As New Outlook.Application
  Dim mItem As Outlook.mailitem  ' An Outlook Mail item
  Set olApp = CreateObject("Outlook.Application")
  Set mItem = olApp.CreateItem(olMailItem)
   
   
  With mItem
      .To = Nz(Me.To, "")
      .CC = Nz(Me.CC, "")
      .BCC = Nz(Me.BCC, "")
      .Subject = Me.SubjectLine
  'add the image in hidden manner, position at 0 will make it hidden
      .Attachments.Add (Me.AttachFile), olByValue, 0
   
      'Now add it to the Html body using image name
      'change the src property to 'cid:your image filename'
      'it will be changed to the correct cid when its sent.
      .HTMLBody = .HTMLBody & "<br><B>Dear Friend:</B><br>" _
  [B][COLOR=red]                & "<img src='cid:doga.jpg'" & "width='500' height='500'><br>" _[/COLOR][/B]
                  & "<br>Best Regards, <br>Your Friendly Advertiser</font></span>"
   
          .Display    ' To show the email message to the user
  End With
   
  End Sub
I would like to know how to substitute the parsed name of the file in the line that has the "cid:doga.jpg'" I know how to get the string "doga.jpg" from the complete file path, but when I try something like

Code:
  Dim Pic as string
  Pic = [FONT=&quot]Right(Me.AttachFile, Len(Me.AttachFile) - InStrRev(Me.AttachFile, "\"))[/FONT]

[FONT=&quot]And substitute Pic for doga.jpg the jpg file no longer appears in the body of the email. The error message on the email says that the image cannot be displayed. [/FONT]

[FONT=&quot]I have tried to substitute the code for getting the Pic value with a hidden text box my email form, but get the same result. The email says that the picture is not available.[/FONT]

[FONT=&quot]Secondly, how does one insert the [Title] and {Surname} from the record set into the HTML? [/FONT]

[FONT=&quot]As before, any help would be greatly appreciated.[/FONT]
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:39
Joined
Feb 28, 2001
Messages
27,128
Unfortunately, the Dept. of Defense site that was my last place of employment would not allow us to use HTML e-mails. Alos, I wasn't on the web design team. Thus my knowledge of HTML is strictly generic, high-level stuff. I have to step away and hope that others with more HTML expertise might be able to take you farther down this path.
 

isladogs

MVP / VIP
Local time
Today, 22:39
Joined
Jan 14, 2017
Messages
18,209
I'm assuming you want something like this.
I've also included text formatting as an example if you want it:




This is the relevant code I used:
Code:
  aSubject = "Test HTML Email message - no attachments"
   ' aTextBody = "This is a test message to check email with no attachments from " & GetProgramName()
    aHTMLBody = "This is a <FONT size=5><FONT color=#800000><B>TEST message</B><FONT size=3>.<FONT color=#000000>" & _
    " to check <I>HTML email</I> with no attachments from the <B><I>" & GetProgramName() & "</B></I> program" & _
    " <P><IMG border=0 hspace=0 alt='' src='file://G:/Programs/MendipDataSystems/CommonFiles/SDA/Images/SDAUpdater1.gif' align=baseline></P>

As you can see, you need the full file path in the <img src> line
The first line should be something like that below:

Code:
.HTMLBody = .HTMLBody & "<br><B>Dear " & Me.Title & " " & Me.LasdtName & " : "</B><br>" _
                  & "<img src='cid:doga.jpg'" & "width='500' height='500'><br>" _
                  & "<br>Best Regards, <br>Your Friendly Advertiser</font></span>"
 

Attachments

  • TEST HTML Email.PNG
    TEST HTML Email.PNG
    30.8 KB · Views: 4,479

Eljefegeneo

Still trying to learn
Local time
Today, 14:39
Joined
Jan 10, 2011
Messages
904
Thank you. After banking my head on my desk for several hours last night, I knew someone could figure it out. I had to change your code slightly to work as it seems that either I wasn’t copying it correctly or something was amiss, but now it reads:

Code:
  .HTMLBody = .HTMLBody & B & "<br><br>Dear " & Me.txtTitle & " " & Me.txtSurname & ": <br>" _
                  & "<img src= 'cid:doga.jpg'" & "width='500' height='500'><br>" _
                  & "<br>Best Regards, <br><br>Your Friendly Advertiser</font></span>"
The last thing I need for this to work perfectly (if that is ever the case) is to be able to change the line:
Code:
                  & "<img src= 'cid:doga.jpg'" & "width='500' height='500'><br>" _
Where
Code:
  "<img src= 'cid:doga.jpg'"
Can be changed at will and not hard coded. For example, If my [me.Attachfile] in the code line
Code:
  .Attachments.Add (me.AttachFile), olByValue, 0
Is "C:\Users\Gene\Pictures\Bird.jpg" instead of "C:\Users\Gene\Pictures\doga.jpg", then I want Bird.jpg to appear in the line instead of doga.jpg. It is possible to use the same logic as you gave me for the "Dear Mr. Jones" line? I have tried various things without success, but then I have no knowledge of HTML coding.
In essence, I want to be able to select a file path and then insert it into the code for the jpg file to appear in the body of the email. This is the question I posed in post #6.
 

Cronk

Registered User.
Local time
Tomorrow, 07:39
Joined
Jul 4, 2013
Messages
2,771
I've never used the cid bit. I think cid involves some pre definition of the string which includes the path to the file so you only need to give the name of the reference.

I use
......<img scr='" & strFullPathAndFileName & "'/>.....
 

isladogs

MVP / VIP
Local time
Today, 22:39
Joined
Jan 14, 2017
Messages
18,209
It seems to me that you are still not entering the full file path - compare your code with mine:

Code:
" <P><IMG border=0 hspace=0 alt='' [COLOR="Red"]src='file://G:/Programs/MendipDataSystems/CommonFiles/SDA/Images/SDAUpdater1.gif'[/COLOR] align=baseline></P>

However I don't understand what the 'cid' bit means so perhaps I'm wrong.

The way I would adapt would be to store the filepath as a new 'ImagePath' field for each record. You could then create a hidden textbox txtImagePath to contain that value or use a variable strImagePath for that purpose

That value would then be used in the 'img src' section with suitable delimiters for text if needed
 

Eljefegeneo

Still trying to learn
Local time
Today, 14:39
Joined
Jan 10, 2011
Messages
904
I will try with your suggestions, thanks to both of you. The cid bit comes from the code I copied from this:

[FONT=&quot][FONT=&quot]http://excel-macro.tutorialhorizon.com/excel-vba-send-mail-with-embedded-image-in-message-body-from-ms-outlook-using-excel/


[/FONT][/FONT]
As I said previously, I have no knowledge of HTML coding, so was winging it.

Will try your suggestions tomorrow as I am not at my home computer right now.
 

Eljefegeneo

Still trying to learn
Local time
Today, 14:39
Joined
Jan 10, 2011
Messages
904
I think I have finally got the code to where I can choose a file to insert into the body of the email. Thanks to all who helped me on this.

After selecting file (jpg) to insert, I parsed the file by using the public function:

Code:
  Public Function FileNameFromPath(strFullPath As String) As String
      FileNameFromPath = Right(strFullPath, Len(strFullPath) - InStrRev(strFullPath, "\"))
  End Function
Then I can add it to "<img src= 'cid:

Code:
  A = FileNameFromPath(Me.AttachFile)
  'Sets the string value of A
  A = "<img src= 'cid:" & A & Chr(39) & ""
The result is the correct string being added to "<img src= 'cid:

This code only seems to work for jpg and I found by accident a .gif file, although the latter took some time to insert into the email. I still cannot figure out how to insert a Pdf file.

The .Attachments can be deleted if the user doesn’t want to add the attachment.
The .Send can be added instead of the .Display

The sizing of the picture is something to be worked out. In my case, I have to determine what the correct dimensions should be and change the width and height dimension to fit the flyers that the marketing department produces.

If anyone sees any glaring mistakes or easy simplifications, please let me know.



Code:
  Private Sub cndSendPictureInBodyOfEmail_Click()
  Dim olApp As New Outlook.Application
  Dim mItem As Outlook.mailitem  ' An Outlook Mail item
  Set olApp = CreateObject("Outlook.Application")
  Set mItem = olApp.CreateItem(olMailItem)
  Dim A, B As String
  'Parses the string from the file name
  A = FileNameFromPath(Me.AttachFile)
  'Sets the string value of A
  A = "<img src= 'cid:" & A & Chr(39) & ""
  B = Format(Date, "Long Date")
   
  With mItem
      .To = Nz(Me.To, "")
      .CC = Nz(Me.CC, "")
      .BCC = Nz(Me.BCC, "")
      .Subject = Me.SubjectLine
      '.Body = Me.MessageBody
      .Attachments.Add (Me.AttachFile) 'Attaches file to email so user can download it for printing.
   
   
  .Attachments.Add (Me.AttachFile), olByValue, 0
   
      'Now add it to the Html body using image name
      'change the src property to 'cid:your image filename'
      'it will be changed to the correct cid when its sent.
      
      .HTMLBody = .HTMLBody & B & "<br><br>Dear " & Me.txtTitle & " " & Me.txtSurname & ": <br>" _
                  & A & "width='1200' height='1200'><br>" _
                  & "<br>Best Regards, <br><br>Your Friendly Advertiser</font></span>"
   
          .Display    ' To show the email message to the user
  End With
  End Sub
 

isladogs

MVP / VIP
Local time
Today, 22:39
Joined
Jan 14, 2017
Messages
18,209
I still cannot figure out how to insert a Pdf file.

You can't put a PDF file in the body of an HTML email - its NOT possible.
The ONLY way to do this is to convert the PDF to an image file
 

Eljefegeneo

Still trying to learn
Local time
Today, 14:39
Joined
Jan 10, 2011
Messages
904
Yes, so it would seem. But I am so stubborn that I keep trying and hoping.

Thanks for the input.
 

isladogs

MVP / VIP
Local time
Today, 22:39
Joined
Jan 14, 2017
Messages
18,209

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:39
Joined
Feb 28, 2001
Messages
27,128
I explained this earlier. Forgive me for being pedantic.

SMTP is a text-oriented method. Therefore, you can send ordinary text as a message body. Everybody can receive text (even SMS-enabled phones).

HTML is a text-oriented method, so you can send HTML as a message body and, if the receiver allows it, the HTML can be processed on the other end. For security reasons, many offices DO NOT allow HTML processing in e-mail. For instance, the U.S. Dept. of Defense disallows HTML mail bodies.

HTML allows you to send images as attachments and DISPLAY them as though they were part of a message body. However, the images are actually not there. What is there is an HTML reference to the image file that is to be inserted in the display at that location - essentially a place-holder.

A .PDF file is NOT an image (it is more complex than that) so HTML is not going to do what you ask. There is no PDF converter associated with HTML, and besides that, the general nature of .PDF files is that they could conceivably be multi-page. There is no mechanism in HTML to handle that except to allow you to download your file using HTTPS or FTP protocols and let you open it locally. But NOT through the message processor.

I'm not trying to be mean or rude, but Colin and I have been telling you that messages are limited in what they can do and you are currently bouncing off of those limits. To spare your head from bruising by butting against a brick wall, please listen.

And if any HTML guru here knows a direct command for .PDF substitution display (as opposed to converting to JPG first), I would be happy to have learned something new and I don't doubt Colin would also enjoy a learning experience.

Further note that when you read Colin's two suggested commercial services, their web pages CLEARLY state that while they take .DOC, .DOCX, and .PDF inputs, they CONVERT them. The first reference expressly states this and the second reference mentions conversion, though in less detail.
 

Users who are viewing this thread

Top Bottom