Print out hyperlinked PDF files from excel list

mohamedboussaida

New Member
Joined
May 26, 2017
Messages
8
Hello Experts;
can somebody help me to find out a way to print a batch of PDF files saved in my desktop from a hyperlinked sheet in excel

in the excel sheet i have a column "A" where listed all the documents with a hyperlink to their related files; the names in the list is same as in the folder

Column A

Row 1
20000000 (in the folder, the file name is 20000000.PDF)
Row 2 20000001 (in the folder, the file name is 20000001.PDF)
Row 3 20000002 (in the folder, the file name is 20000000.PDF)

what i want is a macro or VBA which will print in the default printer the full batch from excel (means the logic is to open the PDF file in the folder then print out through the default print then close the file, and then the to the next file...)

i appreciate your help

i have more than 5000 files, if i do it one by one is too difficult
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hello again, I just want to make it more clear:
can somebody help me to find out a way to print a batch of PDF files saved in my desktop from a hyperlinked sheet in excel

in the excel sheet i have a column "A" where listed all the documents with a hyperlink to their related files; the names in the list is same as in the folder

Column A
20000000 (in the folder, the file name is 20000000.PDF)
20000001 (in the folder, the file name is 20000001.PDF)
20000002 (in the folder, the file name is 20000000.PDF)

what i want is a macro or VBA which will print in the default printer the full batch from excel (means the logic is to open the PDF file in the folder then print out through the default print then close the file, and then the to the next file...)

i appreciate your help

i have more than 5000 files, if i do it one by one is too difficult
 
Upvote 0
Firstly. You don't need to open the pdf files.

This code, taken from a post in Ozgrid, should give you a helping hand.


Code:
Option Explicit
 
Declare Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long
 
Public Sub PrintFile(ByVal strPathAndFilename As String)
 
    Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0)
 
End Sub
 
Sub Test()
    PrintFile ("C:\Test.pdf")
End Sub

You will need to create a Sub (or modify sub Test()) to loop through all the cells, read the file name and then call the PrintFile sub

If you need help with that come back and let us know if the files are all in the same folder and if not does the hyperlink have the full pathname?
 
Upvote 0
Thanks a lot for your swift reply Mr. Stiuart_W

actually all my files are located in the same folder in PDF form,
D:\WORK\Archiving Managment\2017\Journals\KP Documents\

Yes I agree with you no need to open the files.

i tried the above vba code but couldn't manage to run it. i just want to print all the listed documents in my excel which they are already hyperlinked to the folder D:\WORK\Archiving Managment\2017\Journals\KP Documents\
each cell in column A include a number such 10000001 which is hyperlinked to 10000001.pdf in D:\WORK\Archiving Managment\2017\Journals\KP Documents\
i have almost 5000 files with the same principle, to print all these docs one by one is very hard, so i am requesting a code vba to print all the hyperlinked files in one time
i am not that expert in vba Sir, this is why i want a ready code to put it to my excel file sorry for disturbing you
thank you so much for accepting helping me
 
Upvote 0
Assuming your list has no header (Ie first cell has a file name) (if it doesn't just change the value of i to the first row).

Just run the LoopandPrintFiles macro
Code:
Option Explicit
 
Declare Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long
 
Public Sub PrintFile(ByVal strPathAndFilename As String)
 
    Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0)
 
End Sub
 
Sub LoopandPrintFiles()
    Dim i As Integer
    Dim FileName, FullFileName As String
    
    i = 1 'Assume that first filename is in cell A1
    Do While Cells(i, 1) <> ""
        FileName = Cells(i, 1).Value
        FullFileName = "D:\WORK\Archiving Managment\2017\Journals\KP Documents\" & FileName & ".pdf"
        PrintFile (FullFileName)
        i = 1 + 1
    Loop
End Sub
 
Last edited:
Upvote 0
Try this

Code:
Declare PtrSafe Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hwnd As LongPtr, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long

Public Sub PrintFile(ByVal strPathAndFilename As String)
 
    Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0)
 
End Sub
 
Sub LoopandPrintFiles()
    Dim i As Integer
    Dim FileName, FullFileName As String
    
    i = 1 'Assume that first filename is in cell A1
    Do While Cells(i, 1) <> ""
        FileName = Cells(i, 1).Value
        FullFileName = "D:\WORK\Archiving Managment\2017\Journals\KP Documents\" & FileName & ".pdf"
        PrintFile (FullFileName)
        i = 1 + 1
    Loop
End Sub

I haven't tested it!
 
Upvote 0
Hello Sir,

the code now is running but it is printing only the first file many times, the file seating in A1 (column A, Row 1) it is not reading the rest of files. i did it twice, it printed first file 8 times, then i run it again, it printed it 12 times (same file)
thank you Sir for your patience
 
Upvote 0
Code:
Declare PtrSafe Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
    ByVal hwnd As LongPtr, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long

Public Sub PrintFile(ByVal strPathAndFilename As String)
 
    Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0)
 
End Sub
 
Sub LoopandPrintFiles()
    Dim i As Integer
    Dim FileName, FullFileName As String
    
    i = 1 'Assume that first filename is in cell A1
    Do While Cells(i, 1) <> ""
        FileName = Cells(i, 1).Value
        FullFileName = "D:\WORK\Archiving Managment\2017\Journals\KP Documents\" & FileName & ".pdf"
        PrintFile (FullFileName)
        i = i + 1
    Loop
End Sub

Sorry!


Should be i = i+1
 
Upvote 0
Thank you for the above code. I have the same issue, but i have small modification if you can help me out. First, all the pdf hyperlinks are in row 13 starting with D13, E13, F13... and I only want to print the pdfs of the visible columns and not the hidden ones. Can you tell me how to proceed? Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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