# Browse to Folder and Print PDF Files



## damaniam1604 (Dec 29, 2022)

Hi,

I need to be able to browse to a folder in a variety of directories, select the desired folder and print all the PDF files within it. There are no subfolders to contend with. The folders contain only PDF's and I am good with it printing to the default printer. 

I have looked over a few other posts, but nothing seems to fit my scenario. Probably because I don't know VBA that well in order to interpret and modify it. Any assistance would be greatly appreciated. Thanks in advance.


----------



## John_w (Dec 29, 2022)

See if this gets you started.  It prints all the PDF files in the specified folder to the default printer.  But note that this will work only if Print is an option in the File Explorer context menu when you right-click a PDF file.


```
Public Sub Print_PDFs()

    Dim Sh32 As Object 'Shell32.Shell
    Dim ShFolderItem As Object 'Shell32.FolderItem
    Dim PDFsFolder As String
    
    PDFsFolder = "C:\path\to\folder\"
   
    Set Sh32 = CreateObject("Shell.Application")    
    For Each ShFolderItem In Sh32.Namespace(CVar(PDFsFolder)).Items
        If LCase(ShFolderItem.Path) Like "*.pdf" Then
            ShFolderItem.InvokeVerb "Print"
        End If
    Next

End Sub
```
The code could be modified to use `Application.FileDialog(msoFileDialogFolderPicker)` to browse for a folder, instead of specifying it in the code.


----------



## damaniam1604 (Dec 30, 2022)

The code worked perfectly once I specified the directory. Could you give me insight on where I would modify the code in order to browse to the folder?


----------



## John_w (Dec 30, 2022)

Replace the PDFsFolder line with:

```
With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select PDFs folder"
        .InitialFileName = ThisWorkbook.Path  'initial folder
        If .Show Then
            PDFsFolder = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With
```


----------

