0

I want to embed a PDF into the body of an email using VBA.

Is there code to insert → Object → Create From file? It seems impossible, so I tried to find a workaround.

I created a separate VBA code which takes the PDF, inserts it into a PowerPoint slide as a shape, then saves that PowerPoint as a JPEG. After I embedded the JPEG into Outlook, the quality is terrible.

How could I fix the quality, or do the same thing in Word in hopes that it will be better quality?

Code for Outlook:

Dim R As Range

For Each R In RList
    Set EItem = EApp.CreateItem(0)
    With EItem
        .display
        .to = R.Offset(0, 2)
        .Subject = "Report Available: " & R.Offset(0, 4)
        .Attachments.Add (path & R.Offset(0, 3))
        .htmlbody = "Dear " & R & " " & R.Offset(0, 1) & ",</br><br>" & "</br><br>" _
          & "Please find your monthly" & " " & "report attached." & " " & Format(Date, "(mm/dd/yyyy)") & _
          "</br><br>" & "</br><br>" & "Regards," & "</br><br>" & _
          "xxx and xxx" & "</br><br>" & .htmlbody & "<center><img src='C:\Users\xxx\Desktop\pdf slips\TheOpen.pdf.jpg' <div align=center> &"
    End With
Next R

Set EApp = Nothing
Set EItem = Nothing

End Sub

Code for PowerPoint:

Sub PDFToPP10()

Dim oSh As Shape
Dim oSl As Slide
Dim sFileName As String
sFileName = "C:\Users\xxx\Desktop\pdf slips\theopen.pdf"
Set oSl = ActivePresentation.Slides(1)
Set oSh = oSl.Shapes.AddOLEObject(Left:=280, _
  Top:=0, _
  Width:=8.5 * 49, _
  Height:=11# * 49, _
  FileName:=sFileName, _
  Link:=msoFalse)

Dim sImagePath As String
Dim sImageName As String
Dim oSlide As Slide
Dim lScaleWidth As Long
Dim lScaleHeight As Long
On Error GoTo Err_ImageSave

sImagePath = "C:\Users\xxx\Desktop\pdf slips\theopen.pdf"
For Each oSlide In ActivePresentation.Slides
    sImageName = TheOpen & ".jpg"
    oSlide.Export sImagePath & sImageName, "JPG"
Next oSlide

Err_ImageSave:
If Err <> 0 Then
    MsgBox Err.Description
End If
End Sub
double-beep
  • 5,031
  • 17
  • 33
  • 41
Mike
  • 1
  • 1
    Email programs use plain text or HTML to format messages. Since your PDF is probably created from a program like Word, why not try converting the original Word (or other program) file to HTML and sending that? – John Korchok Jul 11 '22 at 20:41
  • Hi John, unfortunately the pdf is not made by me, and I just receive it. I was thinking of trying to do PDF-->Excelsheet-->PNG, but am worried that I will lose photos (another complication). Also, I originally made an HTML page which consisted of a background with the PDF embedded, but I could not get it into the email, it would show up completely blank. – Mike Jul 12 '22 at 14:37
  • Adobe Acrobat can convert PDF to HTML. Once you have it installed, you should be able to script it to automate the conversion. I wouldn't bother with graphic conversions using Excel or Word, they are graphically inept compared to PowerPoint. – John Korchok Jul 12 '22 at 15:02
  • is this what you are trying to do? https://stackoverflow.com/a/44599739/4539709 – 0m3r Jul 13 '22 at 23:52
  • @0M3R, so close but not quite, do you have an email that I can send you an example? (just cant send a screenshot in this) – Mike Jul 15 '22 at 16:49
  • another example https://stackoverflow.com/a/48897439/4539709 maybe? if not just post screenshot link – 0m3r Jul 15 '22 at 23:10
  • -https://paste.pics/58b85795ed6369fc6526de6af04420b9, there is the link. My big issue is that the pdf is coming to me, if I were making it my life would be much easier as I could take it directly from excel. – Mike Jul 18 '22 at 14:22
  • Well, you are adding an image referencing a local file - `C:\Users\xxx\Desktop\pdf slips\TheOpen.pdf.jpg` - there is no way a recipient will be able to see it. – Dmitry Streblechenko Feb 28 '23 at 19:46

0 Answers0