0% found this document useful (0 votes)
97 views

Functions Module

This document contains code for three functions used to create and share PDF documents from Excel workbooks. The first function, RDB_Create_PDF, takes a workbook object, file path, and other parameters to export the workbook as a PDF. The second function, RDB_Mail_PDF_Outlook, is used to email the generated PDF using Outlook with options for recipients, subject, body, and attachments. The third function, Create_PDF_Sheet_Level_Names, creates a PDF containing only the sheets in a workbook that have a specific named range, and returns the file path if successful.

Uploaded by

mts almuttaqin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Functions Module

This document contains code for three functions used to create and share PDF documents from Excel workbooks. The first function, RDB_Create_PDF, takes a workbook object, file path, and other parameters to export the workbook as a PDF. The second function, RDB_Mail_PDF_Outlook, is used to email the generated PDF using Outlook with options for recipients, subject, body, and attachments. The third function, Create_PDF_Sheet_Level_Names, creates a PDF containing only the sheets in a workbook that have a specific named range, and returns the file path if successful.

Uploaded by

mts almuttaqin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Attribute VB_Name = "FunctionsModule"

Option Explicit

'The code below are used by the macros in the other two modules
'Do not change the code in the functions in this module

Function RDB_Create_PDF(Source 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
Source.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, _


StrCC As String, StrBCC As String, StrSubject As
String, _
Signature As Boolean, Send As Boolean, StrBody As
String)
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next


With OutMail
If Signature = True Then .Display
.To = StrTo
.CC = StrCC
.BCC = StrBCC
.Subject = StrSubject
.HTMLBody = StrBody & "<br>" & .HTMLBody
.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,
OpenPDFAfterPublish 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

You might also like