View Single Post
 
Old 10-01-2014, 11:07 PM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

To send the PDF by e-mail using the following code assumes the sender has Outlook.
I have left the code displaying the message requiring the user to send it.
You can add the command to send the message if you wish, after .Display (i.e. .Send) but retain .Display:

Code:
Sub Send_PDF_As_Attachment()
Dim oOutlookApp As Object
Dim oItem As Object
Dim olInsp As Object
Dim strFname As String
Dim wdDoc As Document
Dim oRng As Range

    With ActiveDocument
        strFname = .SaveAs2(Filename:="C:\Users\" & Environ("Username") & "\Documents\" & _
         .SelectContentControlsByTitle("MyControl")(1).Range.Text & ".PDF", _
         FileFormat:=wdFormatPDF)
    End With

    On Error Resume Next
    'Get Outlook if it's running
    Set oOutlookApp = GetObject(, "Outlook.Application")

    'Outlook wasn't running, start it from code
    If Err <> 0 Then
        Set oOutlookApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0
    'Create a new mailitem
    Set oItem = oOutlookApp.CreateItem(0)

    With oItem
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        Set oRng = wdDoc.Range
        oRng.Collapse 1
        oRng.Text = "This is the message body"
        .to = "someone@somewhere.com"
        .Subject = "This is the subject"
        .BodyFormat = 2
        .Attachments.Add strFname
        .Display
    End With

    'Clean up
    Set oItem = Nothing
    Set oOutlookApp = Nothing
    Set olInsp = Nothing
    Set wdDoc = Nothing
    Set oRng = Nothing
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote