Save VBA to PDF

Accounts

New Member
Joined
Jul 10, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have the below code that works perfectly to generate multiple forms based on a table data. However the forms produced are in XLS format and i would like them to automatically save to PDF. How can i amend the code to automatically generate PDF forms rather than excel documents?


Option Explicit

Sub GenerateForms()
Dim wSrc As Workbook
Dim sSrc As Worksheet
Dim wTrg As Workbook
Dim sTrg As Worksheet
Dim r As Long
Dim m As Long
Application.ScreenUpdating = False
Set wSrc = ThisWorkbook
Set sSrc = wSrc.Worksheets("Passenger Vehicles")
m = sSrc.Range("B" & sSrc.Rows.Count).End(xlUp).Row
For r = 2 To m
Select Case sSrc.Range("A" & r).Value
Case "Standard"
wSrc.Worksheets("Declaration").Copy
Case "Supervisor"
wSrc.Worksheets("Supervisory Review").Copy
End Select
Set wTrg = ActiveWorkbook
Set sTrg = wTrg.Worksheets(1)
sTrg.Range("F6").Value = sSrc.Range("E" & r).Value
sTrg.Range("C8").Value = sSrc.Range("G" & r).Value
sTrg.Range("F8").Value = sSrc.Range("H" & r).Value
sTrg.Range("C9").Value = sSrc.Range("C" & r).Value
sTrg.Range("F9").Value = sSrc.Range("I" & r).Value
sTrg.Range("C10").Value = sSrc.Range("K" & r).Value
sTrg.Range("F10").Value = sSrc.Range("J" & r).Value
sTrg.Range("C27").Value = sSrc.Range("E" & r).Value
sTrg.Range("F27").Value = sSrc.Range("D" & r).Value
' Optional: save and close the review workbook
wTrg.SaveAs Filename:=wSrc.Path & "\" & sSrc.Range("B" & r).Value & ".PDF", _
FileFormat:=xlTypePDF
wTrg.Close SaveChanges:=False
Next r
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hello @Accounts.
VBA Code:
        ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' The rest of your code is above
        sTrg.Range("F27").Value = sSrc.Range("D" & r).Value

        ' Construct the file name
        Dim fileName As String: fileName = wSrc.Path & "\" & sSrc.Range("B" & r).Value & ".pdf"

        ' Save the worksheet as a PDF
        wTrg.ExportAsFixedFormat Type:=xlTypePDF, fileName:=fileName, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False

        MsgBox "PDF has been saved as " & fileName

        ' Optional: save and close the review workbook
        '        wTrg.SaveAs Filename:=wSrc.Path & "\" & sSrc.Range("B" & r).Value & ".PDF", _
                 FileFormat:=xlTypePDF
        '        wTrg.Close SaveChanges:=False
    Next r

    Application.ScreenUpdating = True
End Sub
Good luck.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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