Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
925 views
in Technique[技术] by (71.8m points)

vba - How to Embed a PDF Document in an Email Message

I am trying embed PDF into body of my email .

I tried following code but it keeps opening word but attaches pdf file but does not embed pdf as a object in the body of email. Any help on this will be appreciated.

Public Sub CreateNewMessage()
Dim objMsg As MailItem

Set objMsg = Application.CreateItem(olMailItem)

 With objMsg
  .To = "test@tst.com"
  .Subject = "This is the subject"
  .BodyFormat = olFormatHTML
            .Attachments.Add ("C:WorkDashbaord.pdf"), olOLE  'Attach PDF File
  'Embed PDF
         Set wordapp = CreateObject("word.Application")
            wordapp.Documents.Open FileName:="C:Work" & "Dashbaord.pdf"
            wordapp.Visible = True



        'Embed PDF
        wordapp.Visible = True
        Set wordapp = GetObject(, "Word.Application")
        wordapp.Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.11", _
        FileName:="C:WorkDashbaord & ".pdf", LinkToFile:=False, _
        DisplayAsIcon:=False

  .Display
End With

Set objMsg = Nothing
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Should be something like this.

Public Sub InsetObject()
    Dim Inspector As Outlook.Inspector
    Dim wdDoc As Word.Document
    Dim Selection As Word.Selection
    Dim Email As Outlook.mailitem

    Set Email = Application.CreateItem(olMailItem)

    With Email
        .To = "0m3r@Email.com"
        .subject = "This is the subject"
        .Attachments.Add ("C:TempTempFile.pdf")
        .Display

         Set Inspector = Application.ActiveInspector()
         Set wdDoc = Inspector.WordEditor
         Set Selection = wdDoc.Application.Selection

         Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.DC", _
                   FileName:="C:TempTempFile.pdf", _
                   LinkToFile:=False, DisplayAsIcon:=False

    End With

    Set Inspector = Nothing
    Set wdDoc = Nothing
    Set Selection = Nothing
End Sub

enter image description here


InlineShapes.AddOLEObject Method (Word)

Creates an OLE object. Returns the InlineShape object that represents the new OLE object.


InlineShape Object (Word)

Represents an object in the text layer of a document. An inline shape can only be a picture, an OLE object, or an ActiveX control. The InlineShape object is a member of the InlineShapes collection. The InlineShapes collection contains all the shapes that appear inline in a document, range, or selection. InlineShape objects are treated like characters and are positioned as characters within a line of text.


Reference to Microsoft Word xx.x Object Library


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...