Print PDF files from folder.

Tofik

Board Regular
Joined
Feb 4, 2021
Messages
114
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi guys, I need a VBA formula which can help me with PDF files PRINTING.

How I thing it should be ? :
1) Excel with button ( Print ) Which print all cells with numbered files.
2) Folder include reports names : 12763-12764_AZF-CS-4004 and in excel I write only AZF-CS-4004. Is it possible to Print in this way or I should write all the file name like 12763-12764_AZF-CS-4004 ?
3) Print sequence to avoid manual sorting printed papers.

1666847603513.png

1666847495153.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I see you are using nitroreader. Do you know how to send a print command to nitroreader?

The filename issue is no problem. Your macro can add "12763-12764_" to each of the filenames before sending the filename to Nitroreader .
 
Upvote 0
I see you are using nitroreader. Do you know how to send a print command to nitroreader?

The filename issue is no problem. Your macro can add "12763-12764_" to each of the filenames before sending the filename to Nitroreader .
To be honest, I don't know how to send a print command to nitroreader.
This Nitro Pdf have not a lot of commands.
The most useful from Nitro PDF for me is only OCR because it rotate automatically and I can avoid routine work.
If you will explain I will appreciate.

P.S I thought no one answer or write to me about this issue. Sorry for late answer.

1667450586530.png
1667450607310.png
 
Upvote 0
This macro should work, as long as Print is shown in the context menu when you right-click on a PDF file in File Explorer, because it uses the Shell32 object.

1) Excel with button ( Print ) Which print all cells with numbered files.
2) Folder include reports names : 12763-12764_AZF-CS-4004 and in excel I write only AZF-CS-4004. Is it possible to Print in this way or I should write all the file name like 12763-12764_AZF-CS-4004 ?
3) Print sequence to avoid manual sorting printed papers.
1) - Assign the macro to the Print button.

2) - Modify the PDFsFolder string to the full path of the folder containing the PDF files. The macro loops through cells on the active sheet in column A starting at row 2 to the last populated cell. The macro looks for the first .pdf file which matches the cell value with the wildcard "*" prepended and prints it if found.

3) - I think this would require a lot more code, probably using Windows API functions to ensure the current PDF has finished spooling to the printer before printing the next PDF. You could try manually changing the printer properties to print directly to the printer instead of spooling. That way, the PDFs should print in the order of the cells, although I haven't tried this. Regardless, I would test the code first by pausing the default printer's print queue and verify that the macro is sending the correct PDFs to the printer, to avoid wasting paper.

VBA Code:
Public Sub Print_PDFs()

    Dim PDFsFolder As String
    Dim PDFfile As String
    Dim r As Long
   
    PDFsFolder = "C:\folder\path\PDFs folder\"      'CHANGE THIS
   
    If Right(PDFsFolder, 1) <> "\" Then PDFsFolder = PDFsFolder & "\"
   
    With ActiveSheet
        For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
            PDFfile = Dir(PDFsFolder & "*" & .Cells(r, "A").Value & ".pdf")
            If PDFfile <> vbNullString Then
                Print_PDF PDFsFolder & PDFfile
            End If
        Next
    End With
   
End Sub


Private Sub Print_PDF(PDFfile As String)
   
    Static Sh As Object 'Shell32.Shell
    Dim folder As Variant, fileName As Variant
   
    folder = Left(PDFfile, InStrRev(PDFfile, "\"))
    fileName = Mid(PDFfile, InStrRev(PDFfile, "\") + 1)
   
    If Sh Is Nothing Then Set Sh = CreateObject("Shell.Application")
    Sh.Namespace(folder).Items.Item(fileName).InvokeVerb "Print"

End Sub
 
Last edited:
Upvote 0

Dear John_w,​

Your code works perfect. But I have the last question regarding paper format. Can I control A4 or A3 manually ? By cell C5 cell = A3 Print and C6 cell = A4 ? If it is possible it will be great for me. Thanks .​

 
Upvote 0
But I have the last question regarding paper format. Can I control A4 or A3 manually ? By cell C5 cell = A3 Print and C6 cell = A4 ?
One way is to assign two different printer names to the same physical printer, one for A4 paper (tray?) and one for A3. Then in VBA set the default Windows printer using:

VBA Code:
Public Sub Test_SetDefaultPrinter()
    SetDefaultPrinter "Printer name for A4"
End Sub


Public Sub SetDefaultPrinter(PrinterName As String, Optional ComputerName As String = ".")

    Dim Printer As Object, Printers As Object, WMIService As Object
    
    Set WMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2")
    Set Printers = WMIService.ExecQuery("Select * from Win32_Printer Where Name = '" & PrinterName & "'")

    For Each Printer In Printers
        Printer.SetDefaultPrinter
    Next

End Sub
 
Upvote 0
hey i am trying to do almost same thing but code errors "Object variable or With block not set"
 
Upvote 0
kod.png

after this line, it gives the error and also it jumps over to the private sub after
VBA Code:
Print_PDF PDFsfolder & PDFfile
do not iterates the loop
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,525
Members
452,651
Latest member
wordsearch

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