Forum Discussion

sayedy1295's avatar
sayedy1295
Copper Contributor
Sep 25, 2024

Vba

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

  • KhaledDardiri's avatar
    KhaledDardiri
    Copper Contributor

    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

Resources