Export an Excel file to PDF using a macro

Created by Compettia Support Team, Modified on Tue, 30 Sep at 1:01 PM by Compettia Support Team


'*********************************************************************************
'Macro que exporta una hoja Excel a formato PDF.

'Versión mínima de Excel: 2010
'**********************************************************************************
'Parámetros:
'sheetname: Nombre de la hoja a exportar.
'pdfname: Nombre para el archivo PDF

Sub ExportaraPDF(sheetname As String, Optional pdfname As String)

' Guarda la hoja especificada o la hoja 1 como un archivo PDF
'
On Error GoTo errcontrol
If sheetname = "" Then
   sheetname = Sheets(1).Name
End If
If pdfname = "" Then
   pdfname = Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".xls")-1)
End If

Sheets(sheetname).Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & pdfname & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Range("A1").Select

Exit Sub

errcontrol:
MsgBox "Se ha producido un error al convertir la hoja " & sheetname & Err.Description

End Sub

 '*********************************************************************************
'Macro que exporta un libro Excel a formato PDF.

'Versión mínima de Excel: 2010
'**********************************************************************************
'Parámetros:
'pdfname: Nombre para el archivo PDF

Sub ExportarLibroaPDF(Optional pdfname As String)

' Guarda el libro como un archivo PDF
'
On Error GoTo errcontrol
If pdfname = "" Then
pdfname = Left(ThisWorkbook.Name, InStr(ThisWorkbook.Name, ".xls") - 1)
End If

ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & pdfname & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Range("A1").Select

Exit Sub

errcontrol:
MsgBox "Se ha producido un error al convertir el libro " & sheetname & Err.Description

End Sub

 

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article