Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The following macro code will export the active sheet (or sheets) in PDF format.
Copy the code to a regular code module, then select the sheet(s) you want to
export, and run the macro.
See the section further down, for details on how the macro works.
NOTE: There are two additional macros below:
Sub PDFActiveSheet()
'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
Next, the macro gets the default path for saving the PDF file. If the active
workbook has been saved, its path is used. If the active workbook has not been
saved, Excel's default save folder is used.
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
The name of the active sheet is cleaned up -- spaces are removed, and periods
are replaced with underscores.
The file path, revised sheet name, and the ".pdf" extension are combined.
The Save As dialog box opens, with the current folder selected, or the default
save folder. The folder is filtered, to show only the PDF files that it contains.
At the top of the Save As window, the customized title is shown, "Select Folder
and FileName to save"
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
The default file name is filled in, and you can overwrite it, to save the file with a
different name. You can also select another folder --just browse to a different
location.
Then, click the Save button, or click Cancel, if you change your mind.
If you click Cancel, the value of myFile is "False", and nothing more
happens -- the macro ends.
If you click Save, the PDF file is created.
Then, if the file was created, the macro shows a confirmation message with the
file path and name.
Sub PDFActiveSheetNoPrompt()
'www.contextures.com
'for Excel 2010 and later
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
On Error GoTo errHandler
strName = wsA.Range("A1").Value _
& " - " & wsA.Range("A2").Value _
& " - " & wsA.Range("A3").Value
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
Macro 3 - No Prompt - File Check
In the macro below, the default name is based on the values in cells A1, A2 and
A3 on the active sheet. The PDF file is automatically saved in the current folder,
with no prompts.
However, if a file with that name already exists in the current folder, a
message asks if you want to overwrite the file. Click Yes or No in the message
box.
NOTE: Be sure to copy the bFileExists Function too, below the main macro
Sub PDFActiveSheetNoPromptCheck()
'www.contextures.com
'for Excel 2010 and later
'checks for existing file
'prompt to overwrite or rename
'uses bFileExists Function, below
strName = wsA.Range("A1").Value _
& " - " & wsA.Range("A2").Value _
& " - " & wsA.Range("A3").Value
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
Else
'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
End If
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
'=============================
Sub Save_as_pdf()
Dim FSO As Object
Dim s(1) As String
Dim sNewFilePath As String
If FSO.FileExists(s(0)) Then
'//Change Excel Extension to PDF extension in FilePath
s(1) = FSO.GetExtensionName(s(0))
If s(1) <> "" Then
s(1) = "." & s(1)
sNewFilePath = Replace(s(0), s(1), ".pdf")
End Sub
1
Private Sub CommandButton1_Click()
2 Application.ScreenUpdating = False
3 ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
4 Filename:="C:\PDF\Export.pdf", _
5 OpenAfterPublish:=False
Application.ScreenUpdating = True
6 End Sub
7
Sub SaveAsPDF()
Application.Screen Updating = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Application.InputBox("Enter File Name")
Application.ScreenUpdating = True
End Sub