Sei sulla pagina 1di 9

'Use your Back button in your browser to go back to the webpage 'http://www.rondebruin.nl/pdf.

htm 'Ron de Bruin, updated 20 Feb 2010 'Note: I add a example to mail each sheet as PDF that have a mail address in A1 'Copy the code in Standard module of your workbook 'If you not know how to do this visit this webpage 'http://www.rondebruin.nl/code.htm

'Start code 'Code to create a PDF file Sub RDB_Workbook_To_PDF() Dim FileName As String 'Call the function with the correct arguments FileName = RDB_Create_PDF(ActiveWorkbook, "", True, True) 'For a fixed file name and overwrite it each time you run the macro use 'RDB_Create_PDF(ActiveWorkbook, "C:\Users\Ron\Test\YourPdfFile.pdf", True, T rue) If FileName <> "" Then 'Ok, you find the PDF where you saved it 'You can call the mail macro here if you want Else MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _ "Microsoft Add-in is not installed" & vbNewLine & _ "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _ "The path to Save the file in arg 2 is not correct" & vbNewLine & _ "You didn't want to overwrite the existing PDF if it exist" End If End Sub

Sub RDB_Worksheet_Or_Worksheets_To_PDF() Dim FileName As String If ActiveWindow.SelectedSheets.Count > 1 Then MsgBox "There is more then one sheet selected," & vbNewLine & _ "be aware that every selected sheet will be published" End If 'Call the function with the correct arguments 'Tip: You can also use Sheets("Sheet3") instead of ActiveSheet in the code(s heet not have to be active then) FileName = RDB_Create_PDF(ActiveSheet, "", True, True) 'For a fixed file name and overwrite it each time you run the macro use 'RDB_Create_PDF(ActiveSheet, "C:\Users\Ron\Test\YourPdfFile.pdf", True, True )

If FileName <> "" Then 'Ok, you find the PDF where you saved it 'You can call the mail macro here if you want Else MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _ "Microsoft Add-in is not installed" & vbNewLine & _ "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _ "The path to Save the file in arg 2 is not correct" & vbNewLine & _ "You didn't want to overwrite the existing PDF if it exist" End If End Sub

Sub RDB_Selection_Range_To_PDF() Dim FileName As String If ActiveWindow.SelectedSheets.Count > 1 Then MsgBox "There is more then one sheet selected," & vbNewLine & _ "ungroup the sheets and try the macro again" Else 'Call the function with the correct arguments 'For a fixed range use this line FileName = RDB_Create_PDF(Range("A10:I15"), "", True, True) 'For the selection use this line 'FileName = RDB_Create_PDF(Selection, "", True, True) 'For a fixed file name and overwrite it each time you run the macro use 'RDB_Create_PDF(Selection, "C:\Users\Ron\Test\YourPdfFile.pdf", True, Tr ue) If FileName <> "" Then 'Ok, you find the PDF where you saved it 'You can call the mail macro here if you want Else MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLi ne & _ "Microsoft Add-in is not installed" & vbNewLine & _ "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _ "The path to Save the file in arg 2 is not correct" & vbNewLi ne & _ "You didn't want to overwrite the existing PDF if it exist" End If End If End Sub

Sub RDB_Sheet_Level_Names_To_PDF() Dim FileName As String 'Call the function with the correct arguments FileName = Create_PDF_Sheet_Level_Names("addtopdf", "", True, True) 'For a fixed file name and overwrite it each time you run the macro use

'Create_PDF_Sheet_Level_Names("addtopdf", _ ' "C:\Users\Ron\Test\YourPdfFile.pdf", True, True) If FileName <> "" Then 'Ok, you find the PDF where you saved it 'You can call the mail macro here if you want Else MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _ "Microsoft Add-in is not installed" & vbNewLine & _ "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _ "The path to Save the file in arg 2 is not correct" & vbNewLine & _ "You didn't want to overwrite the existing PDF if it exist" End If End Sub

'Code to create a PDF and a Mail 'Only working if you use Outlook Sub RDB_Workbook_To_PDF_And_Create_Mail() Dim FileName As String 'Call the function with the correct arguments FileName = RDB_Create_PDF(ActiveWorkbook, "", True, False) 'For a fixed file name and overwrite it each time you run the macro use 'RDB_Create_PDF(ActiveWorkbook, "C:\Users\Ron\Test\YourPdfFile.pdf", True, T rue) If FileName <> "" Then 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", Fal se Else MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _ "Microsoft Add-in is not installed" & vbNewLine & _ "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _ "The path to Save the file in arg 2 is not correct" & vbNewLine & _ "You didn't want to overwrite the existing PDF if it exist" End If End Sub

Sub RDB_Worksheet_Or_Worksheets_To_PDF_And_Create_Mail() Dim FileName As String If ActiveWindow.SelectedSheets.Count > 1 Then MsgBox "There is more then one sheet selected," & vbNewLine & _

"be aware that every selected sheet will be published" End If 'Call the function with the correct arguments 'Tip: You can also use Sheets("Sheet3") instead of ActiveSheet in the code(s heet not have to be active then) FileName = RDB_Create_PDF(ActiveSheet, "", True, False) 'For a fixed file name and overwrite it each time you run the macro use 'RDB_Create_PDF(ActiveSheet, "C:\Users\Ron\Test\YourPdfFile.pdf", True, True ) If FileName <> "" Then 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", Fal se Else MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _ "Microsoft Add-in is not installed" & vbNewLine & _ "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _ "The path to Save the file in arg 2 is not correct" & vbNewLine & _ "You didn't want to overwrite the existing PDF if it exist" End If End Sub

Sub RDB_Selection_Range_To_PDF_And_Create_Mail() Dim FileName As String If ActiveWindow.SelectedSheets.Count > 1 Then MsgBox "There is more then one sheet selected," & vbNewLine & _ "ungroup the sheets and try the macro again" Else 'Call the function with the correct arguments 'For a fixed range use this line FileName = RDB_Create_PDF(Range("A10:I15"), "", True, False) 'For the selection use this line 'FileName = RDB_Create_PDF(Selection, "", True, True) 'For a fixed file name and overwrite it each time you run the macro use 'RDB_Create_PDF(Selection, "C:\Users\Ron\Test\YourPdfFile.pdf", True, Tr ue) If FileName <> "" Then RDB_Mail_PDF_Outlook FileName, "ron@debruin.nl", "This is the subjec t", _ "See the attached PDF file with the last figure s" _ & vbNewLine & vbNewLine & "Regards Ron de bruin", False Else MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLi

ne & _ "Microsoft Add-in is not installed" & vbNewLine & _ "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _ "The path to Save the file in arg 2 is not correct" & vbNewLi ne & _ "You didn't want to overwrite the existing PDF if it exist" End If End If End Sub

Sub RDB_Sheet_Level_Names_To_PDF_And_Create_Mail() Dim FileName As String 'Call the function with the correct arguments FileName = Create_PDF_Sheet_Level_Names("addtopdf", "", True, False) 'For a fixed file name and overwrite it each time you run the macro use 'Create_PDF_Sheet_Level_Names("addtopdf", _ ' "C:\Users\Ron\Test\YourPdfFile.pdf", True, True) If FileName <> "" Then 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", Fal se Else MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _ "Microsoft Add-in is not installed" & vbNewLine & _ "You Canceled the GetSaveAsFilename dialog" & vbNewLine & _ "The path to Save the file in arg 2 is not correct" & vbNewLine & _ "You didn't want to overwrite the existing PDF if it exist" End If End Sub

'Example 5 in this module wil create a PDF of each sheet that have a mail addres s 'in A1 and Mail this sheet to the address in A1. Sub Mail_Every_Worksheet_With_Address_In_A1_PDF() 'Working only in 2007 and up Dim sh As Worksheet Dim TempFilePath As String Dim TempFileName As String Dim FileName As String 'Temporary path to save the PDF files 'You can also use another folder like 'TempFilePath = "C:\Users\Ron\MyFolder\" TempFilePath = Environ$("temp") & "\" 'Loop through every worksheet

For Each sh In ThisWorkbook.Worksheets FileName = "" 'Test A1 for a mail address If sh.Range("A1").Value Like "?*@?*.?*" Then 'If there is a mail address in A1 create the file name and the PDF TempFileName = TempFilePath & "Sheet " & sh.Name & " of " _ & ThisWorkbook.Name & " " _ & Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf" FileName = RDB_Create_PDF(sh, TempFileName, True, False) 'If publishing is OK create the mail If FileName <> "" Then RDB_Mail_PDF_Outlook FileName, sh.Range("A1").Value, "This is th e subject", _ "See the attached PDF file with the last fi gures" _ & vbNewLine & vbNewLine & "Regards Ron de bru in", False 'After the mail is created delete the PDF file in TempFilePath If Dir(TempFileName) <> "" Then Kill TempFileName Else MsgBox "Not possible to create the PDF, possible reasons:" & vbN ewLine & _ "Microsoft Add-in is not installed" & vbNewLine & _ "The path to Save the file in arg 2 is not correct" & vbN ewLine & _ "You didn't want to overwrite the existing PDF if it exis t" End If End If Next sh End Sub

'This are the functions that the macros above will use 'Do not forget to copy them in your module Function RDB_Create_PDF(Myvar As Object, FixedFilePathName As String, _ OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String Dim FileFormatstr As String Dim Fname As Variant 'Test If the Microsoft Add-in is installed If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _ & Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then If FixedFilePathName = "" Then

'Open the GetSaveAsFilename dialog to enter a file name for the pdf FileFormatstr = "PDF Files (*.pdf), *.pdf" Fname = Application.GetSaveAsFilename("", filefilter:=FileFormatstr, _ Title:="Create PDF") 'If you cancel this dialog Exit the function If Fname = False Then Exit Function Else Fname = FixedFilePathName End If 'If OverwriteIfFileExist = False we test if the PDF 'already exist in the folder and Exit the function if that is True If OverwriteIfFileExist = False Then If Dir(Fname) <> "" Then Exit Function End If 'Now the file name is correct we Publish to PDF On Error Resume Next Myvar.ExportAsFixedFormat _ Type:=xlTypePDF, _ FileName:=Fname, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=OpenPDFAfterPublish On Error GoTo 0 'If Publish is Ok the function will return the file name If Dir(Fname) <> "" Then RDB_Create_PDF = Fname End If End Function

Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, _ StrSubject As String, StrBody As String, Send As B oolean) Dim OutApp As Object Dim OutMail As Object Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .To = StrTo .CC = "" .BCC = "" .Subject = StrSubject .Body = StrBody .Attachments.Add FileNamePDF If Send = True Then .Send Else .Display End If End With On Error GoTo 0

Set OutMail = Nothing Set OutApp = Nothing End Function

Function Create_PDF_Sheet_Level_Names(NamedRange As String, FixedFilePathName As String, _ OverwriteIfFileExist As Boolean, OpenPDFAf terPublish As Boolean) As String 'This function will create a PDF with every sheet with 'a sheet level name variable <NamedRange> in it Dim FileFormatstr As String Dim Fname As Variant Dim Ash As Worksheet Dim sh As Worksheet Dim ShArr() As String Dim s As Long Dim SheetLevelName As Name 'Test If the Microsoft Add-in is installed If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _ & Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then 'We fill the Array with sheets with the sheet level name variable For Each sh In ActiveWorkbook.Worksheets If sh.Visible = -1 Then Set SheetLevelName = Nothing On Error Resume Next Set SheetLevelName = sh.Names(NamedRange) On Error GoTo 0 If Not SheetLevelName Is Nothing Then s = s + 1 ReDim Preserve ShArr(1 To s) ShArr(s) = sh.Name End If End If Next sh 'We exit the function If there are no sheets with 'a sheet level name variable named <NamedRange> If s = 0 Then Exit Function If FixedFilePathName = "" Then 'Open the GetSaveAsFilename dialog to enter a file name for the pdf FileFormatstr = "PDF Files (*.pdf), *.pdf" Fname = Application.GetSaveAsFilename("", filefilter:=FileFormatstr, _ Title:="Create PDF") 'If you cancel this dialog Exit the function If Fname = False Then Exit Function Else Fname = FixedFilePathName End If 'If OverwriteIfFileExist = False we test if the PDF

'already exist in the folder and Exit the function if that is True If OverwriteIfFileExist = False Then If Dir(Fname) <> "" Then Exit Function End If Application.ScreenUpdating = False Application.EnableEvents = False 'Remember the ActiveSheet Set Ash = ActiveSheet 'Select the sheets with the sheet level name in it Sheets(ShArr).Select 'Now the file name is correct we Publish to PDF On Error Resume Next ActiveSheet.ExportAsFixedFormat _ Type:=xlTypePDF, _ FileName:=Fname, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=OpenPDFAfterPublish On Error GoTo 0 'If Publish is Ok the function will return the file name If Dir(Fname) <> "" Then Create_PDF_Sheet_Level_Names = Fname End If Ash.Select Application.ScreenUpdating = True Application.EnableEvents = True End If End Function

Potrebbero piacerti anche