You are on page 1of 10

Example code to create PDF or create/mail a PDF with Outlook 24-Nov-09

You can find the latest information and code on this webpage
http://www.rondebruin.nl/pdf.htm

Below you can test the code from the webpage above
Look good at the code in the VBA editor in the "CreatePDF" module

Create PDF examples


1: Create a PDF of the whole workbook
2: Create a PDF of the ActiveSheet or selected sheets (You can also use a fixed sheet, see code how)
3: Create a PDF of the selection or range (I use the range A10:I15 in this example)
4: Create a PDF with every sheet with a specific sheet level name (Sheet2 and sheet4 in this example)

Example 1 Example 2 Example 3 Example 4

Information

The first three macros calls a function named RDB_Create_PDF


Macro four calls a function named Create_PDF_Sheet_Level_Names
You can find this two functions in the module named "FunctionsModule"

You see there are four arguments in the function call.

1: What do we want to publish (in macro four this is the sheet level name)
2: Path/File name of the pdf file, if you use "" it will ask you for a file name.
3: Overwrite the file you choose in the GetSaveAsFilename dialog if it already exist :True or False
4: OpenAfterPublish : True or False
This will create a PDF of the ActiveWorkbook and overwrite the file if it exist and open the PDF.
RDB_Create_PDF(ActiveWorkbook, "", True, True)

Note: For a fixed file name and overwrite it each time you run the macro use this
RDB_Create_PDF(ActiveWorkbook, "C:\Users\Ron\Test\YourPdfFile.pdf", True, True)

This will create a PDF of all the sheets with a sheet level named range "addtopdf" and
overwrite the file if it exist and open the PDF. In this file Sheet2 and Sheet4 have this name.
FileName = Create_PDF_Sheet_Level_Names("addtopdf", "", True, True)

Note: For a fixed file name and overwrite it each time you run the macro use this
Create_PDF_Sheet_Level_Names("addtopdf", "C:\Users\Ron\Test\YourPdfFile.pdf", True, True)

How do I add sheet level names to the sheets I want ?

1: Ctrl-F3 to open the Name manager


2: Click on New
3: Enter addtopdf as name
4: Change scope to the sheet you want
5: Refers to is not important because we only use the name
6: OK
7: Repeat the steps above for every sheet you want in the PDF.

Create and Mail the PDF

Below you can test the code to create the PDF and also create a mail with the PDF attached
Look good at the code in the VBA editor in the "CreatePDFMail" module
Note: The code is only working if you use Outlook

1: Create a PDF and mail of the whole workbook


2: Create a PDF and mail of the ActiveSheet or selected sheets (You can also use a fixed sheet)
3: Create a PDF and mail of the selection or range (I use the range A10:I15 in this example)
4: Create a PDF and mail with every sheet with a specific sheet level name (Sheet2 and sheet4)
5: Create a PDF and mail of every sheet with a mail address in cell A1 (Sheet5 and sheet6)
You see that the code create two mails, one with sheet5 and one with sheet6 and send it to the address in A1 of that sheet.

Example 1 Example 2 Example 3 Example 4 Example 5

I replaced this two comments lines in the code from the create PDF code above

'Ok, you find the PDF where you saved it


'You can call the mail macro here if you want

With this:

RDB_Mail_PDF_Outlook Filename, "ron@debruin.nl", "This is the subject", _


"See the attached PDF file with the last figures" _
& vbNewLine & vbNewLine & "Regards Ron de bruin", False

Tip: If you want to delete the PDF file from your system you can add this code line:
Kill FileName

You see there are five arguments in this macro.

1: FileName (do not change this)


2: To who do we want to send the mail ?
3: What is the subject ?
4: What do we want in the body of the mail ?
5: Do we want to Display (False) the mail or send it directly(True)

Note: I also set the last argument to False of the code line that create the PDF so it
not open the PDF. RDB_Create_PDF(ActiveWorkbook, "", True, False)

For much more mail code examples visit my mail page


http://www.rondebruin.nl/sendmail.htm
Header 1 Header 2 Header 3 Header 4 Header 5
Cell A2 of Sheet1 Cell B2 of Sheet1 Cell C2 of Sheet1 Cell D2 of Sheet1 Cell E2 of Sheet1
Cell A3 of Sheet1 Cell B3 of Sheet1 Cell C3 of Sheet1 Cell D3 of Sheet1 Cell E3 of Sheet1
Cell A4 of Sheet1 Cell B4 of Sheet1 Cell C4 of Sheet1 Cell D4 of Sheet1 Cell E4 of Sheet1
Header 1 Header 2 Header 3 Header 4 Header 5
Cell A2 of Sheet2 Cell B2 of Sheet2 Cell C2 of Sheet2 Cell D2 of Sheet2 Cell E2 of Sheet2
Cell A3 of Sheet2 Cell B3 of Sheet2 Cell C3 of Sheet2 Cell D3 of Sheet2 Cell E3 of Sheet2
Cell A4 of Sheet2 Cell B4 of Sheet2 Cell C4 of Sheet2 Cell D4 of Sheet2 Cell E4 of Sheet2
Header 1 Header 2 Header 3 Header 4 Header 5
Cell A2 of Sheet3 Cell B2 of Sheet3 Cell C2 of Sheet3 Cell D2 of Sheet3 Cell E2 of Sheet3
Cell A3 of Sheet3 Cell B3 of Sheet3 Cell C3 of Sheet3 Cell D3 of Sheet3 Cell E3 of Sheet3
Cell A4 of Sheet3 Cell B4 of Sheet3 Cell C4 of Sheet3 Cell D4 of Sheet3 Cell E4 of Sheet3
Header 1 Header 2 Header 3 Header 4 Header 5
Cell A2 of Sheet4 Cell B2 of Sheet4 Cell C2 of Sheet4 Cell D2 of Sheet4 Cell E2 of Sheet4
Cell A3 of Sheet4 Cell B3 of Sheet4 Cell C3 of Sheet4 Cell D3 of Sheet4 Cell E3 of Sheet4
Cell A4 of Sheet4 Cell B4 of Sheet4 Cell C4 of Sheet4 Cell D4 of Sheet4 Cell E4 of Sheet4
sheet5@something.nl

This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5 This is sheet 5
sheet6@something.nl

This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6
This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6 This is sheet 6

You might also like