# Solved: Printing Multiple PDF files from a folder



## LostinKansas (Feb 25, 2010)

I'm wondering how to print multiple PDF files from a folder. I would like to be able to browse folders, select one then print all PDFs stored in that folder. I have found code supplied by Rollin_Again, that will accomplish this for all Word documents in a folder from a module stored in a word document. Ideally I would like this to be a script I could run from my desktop if possible.

How, if possible, could I alter this code to work for me? Or any other suggestions would be great! Thanks in advance.

The original code is found here: http://forums.techguy.org/business-applications/485943-printing-multiple-files-folder.html


----------



## DaveA (Nov 16, 1999)

I don't use a script, all I do is open Windows Explorer, browse to the folder, select the file(s), and right click and select print. This will print to the DEFAULT printer, so if you want a different printer, then it needs to set as the default.


----------



## Rollin_Again (Sep 4, 2003)

If you want this done automatically through a script you can copy the code below into a blank text document and then manually change the extension to *.vbs* instead of *.txt*

To execute the code just double click on the VBS script and it should print everything that is in the directory regardless of file type. Just change the portion in red to reflect your correct file path.


```
set shApp = createobject("shell.application") 
set shFolder = shApp.namespace("[COLOR="Red"]C:\TEST[/COLOR]") 
set vItems = shFolder.Items() 
for each vItem in vItems 
  vItem.invokeverb "&Print" 
next
```
Rollin


----------



## Rollin_Again (Sep 4, 2003)

The code above is used in a stand alone VBS script with the folder path "hard-coded"

You also have the option of attaching the macro code to any MS Office application as a macro. If you choose this route we can also build in the functionality to allow the user to Browse for the source folder instead.

Rollin


----------



## Noyb (May 25, 2005)

In the other idea category ...
You could merge all pdfs into one pdf ... With the freeware Pdfsam ...
It can also cutout specific pages from a pdf


----------



## LostinKansas (Feb 25, 2010)

Rollin- I tried this code above in your first reply but I received an error message that read as following

Script: C:\Documents and Settings\jkilpatrick\Desktop\PrintNotice.VBS
Line: 3
Char: 5 
Error: Syntax error
Code: 800A03EA
Source: Microsoft VBScript compilation error


I think the second route might work better. I would like to select the folder to print all PDFs from. Here is what i am doing ultimately. I have an Access DB set up that stores data then all the reports are exported to PDF and stored in a file named mm-dd-yyyy (for the date that the PDFs were created). I am looking to add a button that will allow the user to enter the date (which is the file name) for which they want to print all stored PDF reports. I figured that your code would be a good starting off point or a good work around just allowing the user to navigate to that folder and print all.

I hope I am clear enough on my objective... if not let me know i will try to clarify more.


----------



## Rollin_Again (Sep 4, 2003)

Did you copy and paste the latest code above? I had to modify it slightly since the forum filters were picking up some of the words as bad words and replaced them with asterix characters.

Rollin


----------



## LostinKansas (Feb 25, 2010)

Ok yes. That was the issue, i had copied from the original post. So this is now working as a script from my desktop. It is possible to have the user input the last folder to navagate to? The user will always point to this file C:\Documents and Settings\jkilpatrick\Desktop\Noxious Weeds Documents but then the last folder would be the date which they are looking to print i.e. 02-25-2010. so the whole path will be "C:\Documents and Settings\jkilpatrick\Desktop\Noxious Weeds Documents\02-25-2010. Can i have the user input that in this script?


----------



## Rollin_Again (Sep 4, 2003)

I've update the code below. Open your Access database and press *ALT + F11* to open the VB editor. Next click *INSERT >> MODULE* to open a blank code module. Paste the code below into the module window and then save your entire project. Then run the macro called "PrintAll"

Note: You just need to change the portion of the code that is highlighted in red to set the default directory in the directory browse window.


```
Private Const BIF_STATUSTEXT = &H4&
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260

Private Const WM_USER = &H400
Private Const BFFM_INITIALIZED = 1
Private Const BFFM_SELCHANGED = 2
Private Const BFFM_SETSTATUSTEXT = (WM_USER + 100)
Private Const BFFM_SETSELECTION = (WM_USER + 102)

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long

Private Type BrowseInfo
  hWndOwner      As Long
  pIDLRoot       As Long
  pszDisplayName As Long
  lpszTitle      As Long
  ulFlags        As Long
  lpfnCallback   As Long
  lParam         As Long
  iImage         As Long
End Type

Private m_CurrentDirectory As String   'The current directory
'

Public Function BrowseForFolder(Title As String, StartDir As String) As String
  'Opens a Treeview control that displays the directories in a computer

  Dim lpIDList As Long
  Dim szTitle As String
  Dim sBuffer As String
  Dim tBrowseInfo As BrowseInfo
  m_CurrentDirectory = StartDir & vbNullChar

  szTitle = Title
  With tBrowseInfo
 
    .lpszTitle = lstrcat(szTitle, "")
    .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT
    .lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc)  'get address of function.
  End With

  lpIDList = SHBrowseForFolder(tBrowseInfo)
  If (lpIDList) Then
    sBuffer = Space(MAX_PATH)
    SHGetPathFromIDList lpIDList, sBuffer
    sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
    BrowseForFolder = sBuffer
  Else
    BrowseForFolder = ""
  End If
  
End Function
 
Private Function BrowseCallbackProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lp As Long, ByVal pData As Long) As Long
  
  Dim lpIDList As Long
  Dim ret As Long
  Dim sBuffer As String
  
  On Error Resume Next  'Sugested by MS to prevent an error from
                        'propagating back into the calling process.
     
  Select Case uMsg
  
    Case BFFM_INITIALIZED
      Call SendMessage(hWnd, BFFM_SETSELECTION, 1, m_CurrentDirectory)
      
    Case BFFM_SELCHANGED
      sBuffer = Space(MAX_PATH)
      
      ret = SHGetPathFromIDList(lp, sBuffer)
      If ret = 1 Then
        Call SendMessage(hWnd, BFFM_SETSTATUSTEXT, 0, sBuffer)
      End If
      
  End Select
  
  BrowseCallbackProc = 0
  
End Function

' This function allows you to assign a function pointer to a vaiable.
Private Function GetAddressofFunction(add As Long) As Long
  GetAddressofFunction = add
End Function

Public Sub PrintAll()


strDirectory = _
BrowseForFolder("Select A Directory", "[COLOR="Red"]C:\Test[/COLOR]")

If Len(strDirectory) = 0 Then Exit Sub  'user selected cancel
    
    
Set shApp = CreateObject("shell.application")

Set shApp = CreateObject("shell.application")
Set shFolder = shApp.Namespace(strDirectory)
Set vItems = shFolder.Items()
For Each vItem In vItems
  vItem.invokeverb "&Print"
Next
End Sub
```
Regards,
Rollin


----------



## LostinKansas (Feb 25, 2010)

Rollin-

Wow you truly are BRILLIANT when sober! Thank you so much for your help this worked great. I had to make one slight change to the code for access to be able to recognize the Print All () as a macro I had to change it from a Public Sub to a Public Function. As well as its corresponding End Sub to End Function. It is PERFECT!

Thanks a ton

not so *LostinKansas* anymore!


----------



## LostinKansas (Feb 25, 2010)

To Dave A and Noyb, 

I appreciate the feedback, both good info to know. Rollin was right on the mark for my task at hand. Thanks though.

LostinKansas


----------



## Rollin_Again (Sep 4, 2003)

Glad it worked out for you! Please mark the thread as solved and welcome to the TSG Forums!

Rollin


----------

