VBA batch print to PDF

okvals48

New Member
Joined
Sep 27, 2020
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi,

i need help with this VBA code, i would like to print PDF files as one document and not separated. I did try to use CHAT GPT but i always receive error that active x component can not create object.
I have two sheet's one is for data and other one is template from where i would like to print PDF document. As i work in accounting i need this PDF as attachment to invoice we have all sort of data on it.

So idea was that VBA automatically changes invoice ID in field A3, after i give the range that i want to be printed out and then saves all the attachments as one PDF.
Cell A3 changes data in template supplier name, account, date etc.

I tried also printing them directly to printer but i receive the same error.

Thanks in advance for help

VBA Code:
Sub Print_to_PDF()
    Dim startInvoice As Long
    Dim endInvoice As Long
    Dim wsSource As Worksheet
    Dim printRange As Range
    Dim invoiceID As Long
    
    ' Prompt the user to input the start of the range
    startInvoice = InputBox("Enter the starting invoice ID:")
    
    ' Prompt the user to input the end of the range
    endInvoice = InputBox("Enter the ending invoice ID:")
    
    ' Link the start of the range to cell A3 on Sheet2
    Set wsSource = ThisWorkbook.Sheets("Sheet2")
    
    ' Loop through each invoice ID within the specified range
    For invoiceID = startInvoice To endInvoice
        ' Set the invoice ID
        wsSource.Range("A3").Value = invoiceID
        
        ' Define the print range with updated invoice data
        Set printRange = wsSource.Range("C3:J20") ' Adjust this range as needed
        
        ' Print the current invoice as a PDF document
        printRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\slavk\Desktop\Printed_Invoice_" & invoiceID & ".pdf", Quality:=xlQualityStandard, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next invoiceID
End Sub

Template for printing
Test.xlsm
ABCDEFGHIJ
35INVOICE NUMBERG1
4SUPPLIERG
5ID5
6DATE I30.4.2024
7INVOICE DATE20.4.2024
8MATURITY DATE5.5.2024
9
10AMOUNTVATVAT KEYVINCOST CENTERREMARKACCOUNTINVOICE TYP
11AMOUNTVATVAT KEYVINCOST CENTERREMARKACCOUNTINVOICE TYP
12100255U_XXXXXX1120454400011200
13100255R_XXXXXX1120454400011200
Sheet2
Cell Formulas
RangeFormula
D3D3=+XLOOKUP(A3,Sheet1!D:D,Sheet1!F:F)
D4:D8D4=+XLOOKUP($D$3,Sheet1!$F$4:$F$11,XLOOKUP(Sheet2!$C4,Sheet1!$D$3:$R$3,Sheet1!$D$4:$R$11))
C10:J10C10=+CHOOSECOLS(Sheet1!$D$3:$R$3,XMATCH(C11,Sheet1!$D$3:$R$3))
C12:J13C12=+CHOOSECOLS(FILTER(Sheet1!$D$4:$R$11,Sheet1!$F$4:$F$11=Sheet2!$D$3,""),XMATCH(C11,Sheet1!$D$3:$R$3))
Dynamic array formulas.


Data tabel
Test TCZ.xlsm
DEFGHIJKLMNOPQR
3IDSUPPLIERINVOICE NUMBERPOSITIONDATE IINVOICE DATEMATURITY DATEAMOUNTVATVAT KEYVINCOST CENTERREMARKACCOUNTINVOICE TYP
41AA1130.4.202415.4.202430.4.2024100,00255U_XXXXXX1120414400011200
52BB1130.4.202416.4.20241.5.2024100,00255U_XXXXXX1120424400011200
63CC1130.4.202417.4.20242.5.2024100,00255U_XXXXXX1120434400011200
74DD1130.4.202418.4.20243.5.2024100,00255U_XXXXXX1120444400011200
85GG1130.4.202420.4.20245.5.2024100,00255U_XXXXXX1120454400011200
96GG1230.4.202420.4.20245.5.2024100,00255R_XXXXXX1120454400011200
107CC2130.4.202417.4.202445414100255U_XXXXXX1120464400021200
118DD2130.4.202418.4.202445415100255U_XXXXXX1120474400011200
Sheet1
Cell Formulas
RangeFormula
J4:J9J4=+I4+15
L4:L9L4=+K4*1.25-K4
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
The code here should work for you with changes to the sheet names and cell references:

 
Upvote 0
The code here should work for you with changes to the sheet names and cell references:

Thx
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,130
Members
453,021
Latest member
Justyna P

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