Mostafa Nabil
Mostafa Nabil

Reputation: 29

Export to PDF with String and Cell Value

I need to export excel sheet as PDF named with String and Cell Value for Example XXXY - Cell Value Note: i do not need codes that uses path location for the saving file I tried the following code

Sub IVI_Formatting_Export_Two_PDF ()

Dim strFilename As String

Dim srn As String Dim SWPDF As Worksheet

Set SWPDF = ThisWorkbook.Sheets ("BSS CPES MainPage")

srn = "CPE Main-Page"

strFilename = SWPDF.Range ("F7").Value

Create File name with Warehouse Name

Export2PDF

SWPDF.ExportAsFixedFormat

Type:=xlTypePDF,

Filename:=strFilename & srn,

Quality:=xl QualityStandard,

IncludeDocProperties:=False,

IgnorePrintAreas:=False,

From:=1,

To:=1,

OpenAfterPublish:=True

End Sub

Upvotes: 0

Views: 716

Answers (1)

Harun24hr
Harun24hr

Reputation: 36860

You must specify a path to export PDF. Without path where it will be saved? You can use variable to specify path. Currently I have used same as file path. Your problem was in filename parameter. Try below sub.

Sub IVI_Formatting_Export_Two_PDF()
Dim strFilename As String
Dim srn As String
Dim SWPDF As Worksheet

Set SWPDF = ThisWorkbook.Sheets("BSS CPES MainPage")

srn = "CPE Main-Page"
strFilename = SWPDF.Range("F7").Value

'Create File name with Warehouse Name
'Export2PDF

    SWPDF.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=ThisWorkbook.Path & "\" & strFilename & "_" & srn & ".pdf", _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=False, _
    IgnorePrintAreas:=False, _
    From:=1, _
    To:=1, _
    OpenAfterPublish:=True

'Clear memory
Set SWPDF = Nothing
End Sub

Here Filename:=ThisWorkbook.Path & "\" & strFilename & "_" & srn & ".pdf" will export the PDF to same folder where your workbook is located. Also you must include .pdf extension to export excel sheet as pdf.

Upvotes: 1

Related Questions