Excel Macro (VBA) sending active worksheet in PDF by Outlook

Exceluser2014

New Member
Joined
Mar 17, 2014
Messages
5
Dear all,

Can anyone help me with this problem. I find out a code to create a PDF (with opening the Save As dialog box) from an active worksheet, but I can't find out how to send this PDF by e-mail (Outlook). The code is working till the words 'Set OutApp'.

Please can anyone help me? Just what I want is to send the active worksheet as PDF (as attachment) by email (Outlook). Here the present code.
Code:
Sub SendPDF()
'
' SendPDF Macro
'
    Dim OutApp As Object
    Dim OutMail As Object
    Dim v As Variant
    v = Application.GetSaveAsFilename(Range("E2").Value, "PDF Files (*.pdf), *.pdf")
         
    If VarType(v) <> vbString Then Exit Sub
     
    If Dir(v) <> "" Then
        If MsgBox("File already exists - do you wish to overwrite it?", vbYesNo, "File Exists") = vbNo Then Exit Sub
    End If
     
    With ActiveSheet
        .ExportAsFixedFormat Type:=xlTypePDF, FileName:=v, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, From:=1, To:=3, OpenAfterPublish:=False
    End With
         
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = ""
        .Body = ""
        .Attachments.Add v
        .Send
    End With
    On Error GoTo 0
    
    Set OutMail = Nothing
    Set OutApp = Nothing


    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

Thanks in advance for all your help and suggestions.

Regards,

Gerben
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Did you fill in the "To address" for the email?
 
Upvote 0
At least one of these needs a value
Code:
With OutMail
    .To = ""
    .CC = ""
    .BCC = ""
end With
 
Upvote 0
Thanks for your replies.

Yes I've filled in the "To" and "CC" address, but for now I've made it blank.
I've to delete the .Send code, otherwise the user can't change and edit the subject and the body.

I know the code is correct till Set Outapp. I know the code to send the whole workbook in Excel format, but I've only to send this active worksheet in PDF.
 
Upvote 0
I've to delete the .Send code, otherwise the user can't change and edit the subject and the body.

Use .Display instead of .Send

I know the code is correct till Set Outapp. I know the code to send the whole workbook in Excel format, but I've only to send this active worksheet in PDF.

hence, the code:

.Attachments.Add v

There must be something else going on.
 
Upvote 0
Problem solved. The solution was very simple. Only change .Send in .Display. Thanks Wigi!
Thanks guys for all your help!
 
Upvote 0
No thanks, you're welcome.
 
Upvote 0
Is there a way to do this without using the save as dialog box? Maybe save it to the default temp folder so the only thing the user sees is the email.
 
Upvote 0
Is there a way to do this without using the save as dialog box? Maybe save it to the default temp folder so the only thing the user sees is the email.

Sure. Environ("temp") is the path for the Temp folder.
 
Upvote 0

Forum statistics

Threads
1,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top