(VBA) Printing the Cell Content into PDF file via VBA

vbaquery

New Member
Joined
Apr 17, 2023
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
I would like to write a vba script for printing the content in column d and column e, which each row will (d1 and e1, d2 and e2, etc.) be printed on the same page as a single pdf file. The pdf file name is the corrsponding column a cell. I try to write the code below for the task. However, it fails and i would like to seek help from you guys:cry:

VBA Code:
Sub SaveToPDF()
    Dim lastRow As Long
    Dim fileName As String
    Dim filePath As String
    Dim ws As Worksheet
    Dim pdf As Object
    Dim i As Long
    
    ' Set the worksheet you want to save from
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    ' Determine the last row in the data set
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ' Loop through each row and save to PDF
    For i = 1 To lastRow
        ' Set the PDF file name based on the value in column A
        fileName = ws.Range("A" & i).Value
        
        ' Set the file path to save the PDF to
        filePath = "C:\PDF Files\" & fileName & ".pdf" ' Change this to the directory you want to save to
        
        ' Save the data in columns D and E as a PDF
        With ws.PageSetup
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 1
        End With
        ws.Range("D" & i & ":E" & i).ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=filePath, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
    Next i
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
What does "it fails" mean? Won't compile? Runs but produces wrong/no result? Raises error message?
You want to output 2 cells (D&i and E&i) for every row that has a value in A&i? Your code looks ok to me and it should do that. However, you have not allowed for any header rows so is that your problem?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top