Home » excel » excel – Print all files in folder VBA code not working

excel – Print all files in folder VBA code not working

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to print all files inside a folder with this code but something is not working correctly…

Sub Bulk_Print_From_Folder()

Dim k       As Variant
Dim n       As Variant
Dim oFile   As Object
Dim oFiles  As Object
Dim oFolder As Object
Dim Path    As Variant
Dim vItem   As Object

    Path = "C:\Testing Folder"

    With CreateObject("Shell.Application")
        Set oFolder = .Namespace(Path)
        If oFolder Is Nothing Then
            MsgBox "The Folder :" & vbLf & """" & Path & """ was Not Found.", vbCritical
            Exit Sub
        End If
    End With

    Set oFiles = oFolder.Items
        oFiles.Filter 64, "*.pdf;*.jpg;*.png;*.txt"

        For n = 0 To oFiles.Count - 1
            Set oFile = oFiles.Item(n)
            For k = 0 To oFile.Verbs.Count - 1
                Set vItem = oFile.Verbs.Item(k)
                If Not vItem Is Nothing Then
                    If Replace(vItem, "&", "") = "Print" Then vItem.DoIt
                End If
            Next k
        Next n

     End Sub

Extra Info:

»» I have macros enabled and default printer defined… The printer is working correctly.

»» I noticed that I don’t see the printer icon in Taskbar after hit the button to execute this code. And if it maters, I’m using Office 2007 Ultimate.

»» I know that it finds the folder. I tested by altering the folder name on the code and I got the message

MsgBox "The Folder :" & vbLf & """" & Path & """ was Not Found.", vbCritical

This are my project references:

enter image description here

A’m I missing something?

How to&Answers:

There is nothing wrong with the code.

The problem is with the default program set for opening the image file. If you right click on the image file, you will not see the Print option. To sort this issue, open Control panel and go to Default Apps. Change the default app of Photo Viewer to say Paint. Close Default Apps

Now right click on the image file and you will see the Print option. If you do see Print, then your code will now run as vItem will have &Print

If you have a mix of files in that folder and you do not see a Print option for any particular extention then do what I suggested above. Change the default app till you see the Print in the context menu.

Depending on OS, you may see &Print or &Print out or something in a regional language. You can test that by using this code

For n = 0 To oFiles.Count - 1
    Set oFile = oFiles.Item(n)
    For k = 0 To oFile.Verbs.Count - 1
        Set vItem = oFile.Verbs.Item(k)
        If Not vItem Is Nothing Then
            Debug.Print vItem
        End If
    Next k
Next n

So you may want to change the code

If Replace(vItem, "&", "") = "Print" Then

to

If InStr(1, vItem, "&Print") Then 

or to

InStr(1, vItem, "imprimir", vbTextCompare) '<~~ See Chat below quesiton