Vba

Copper Contributor

Hey all 

I have my excel workbook with a main sheet 

that has the table of contents 

for each content it is hyperlinked with other sheets .

i want to make a vba code to print my workbook as a pdf.

i want the hyperlinks i made in excel to be preserved in the pdf 

I want them to work in the pdf as welll

1 Reply

@sayedy1295 

Sure, insert a new module and paste the following code into it:

Option Explicit

Sub SaveAsPDFWithHyperlinks()
    Dim ws As Worksheet
    Dim pdfPath As String
    Dim pdfFileName As String
    
    ' Set the worksheet you want to save as PDF
    Set ws = ThisWorkbook.Sheets("Main Sheet") ' Change "Main Sheet" to your sheet name
    
    ' Set the path and file name for the PDF
    pdfPath = ThisWorkbook.Path & "\"
    pdfFileName = "YourFileName.pdf" ' Change to your desired file name
    
    ' Export the sheet as PDF
    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath & pdfFileName, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub