VBA needed for save a range as pdf

nahean

New Member
Joined
Jan 6, 2022
Messages
15
Office Version
  1. 2007
Platform
  1. Windows
I am using following macro to save my excel active sheet as pdf and it save the whole active sheet range as pdf. I want to save a certain page (e.g. 2 to 9) or range (e.g. A12:k52) as pdf integrated this following code.


Sub PDFActiveSheetNoPromptCheck()

'checks for existing file
'prompt to overwrite or rename
'uses bFileExists Function, below

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim lOver As Long
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet

'get active workbook folder, if saved
strPath = wbA.path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

strName = wsA.Range("d5").Value _
& "-" & wsA.Range("b25").Value _
& "-" & wsA.Range("d6").Value

'create default name for savng file
strFile = strName & ".pdf"
strPathFile = strPath & strFile

If bFileExists(strPathFile) Then
lOver = MsgBox("Overwrite existing file?", _
vbQuestion + vbYesNo, "File Exists")
If lOver <> vbYes Then
'user can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
strPathFile = myFile
Else
GoTo exitHandler
End If
End If
End If

'export to PDF in current folder
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& strPathFile

exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
'=============================
Function bFileExists(rsFullPath As String) As Boolean
bFileExists = CBool(Len(Dir$(rsFullPath)) > 0)
End Function
'=============================



thanks for helping me out.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi nahean,

Try adding this line...

VBA Code:
From:=2, To:=9, _

immediately below this line:

VBA Code:
IncludeDocProperties:=True, _

Regards,

Robert
 
Upvote 0
wo
Hi nahean,

Try adding this line...

VBA Code:
From:=2, To:=9, _

immediately below this line:

VBA Code:
IncludeDocProperties:=True, _

Regards,

Robert
Thanx a lot, it worked perfectly.

btw, the pdf file is save as the same directory where the excel file exist. Can you add another line to save the file in a other folder (e.g. in a new/existing folder).

Currently the pdf file save in "D:\excel document\"
I want to save this pdf in "D:\excel document\pdf\"

thanx again.
 
Upvote 0
Assuming the directory exists, change this line...

VBA Code:
strPath = strPath & "\"

...to this:

VBA Code:
strPath = strPath & "\pdf\"
 
Upvote 0
Solution

Forum statistics

Threads
1,214,983
Messages
6,122,582
Members
449,089
Latest member
Motoracer88

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