Solved How to Print several PDF files without opening them? (1 Viewer)

deletedT

Guest
Local time
Today, 03:28
Joined
Feb 2, 2019
Messages
1,220
I need to print a series of pdf files.
If I use a loop to go through the files' names and use a shell.excecute to print them, each pdf file is opened, printed and closed. You can guess how it feels if I need to print let's say 20+ files.

A google search gave me the following function, where the author says vba will print the files without opening them. But no.
Still pdf opens (though shorter than shell.execute).

SQL:
Public Sub PrintAnyDocument_WithoutOpening(ByVal strPathFile As String)

    Dim TargetFolder
    Dim FileName
    Dim ObjShell As Object
    Dim ObjFolder As Object
    Dim ObjItem As Object
    Dim ColItems As Object
    
    If InStrRev(strPathFile, "\") <> 0 Then
        TargetFolder = Left(strPathFile, InStrRev(strPathFile, "\"))
        FileName = Right(strPathFile, Len(strPathFile) - Len(TargetFolder))
    End If
    Set ObjShell = CreateObject("Shell.Application")
    Set ObjFolder = ObjShell.NameSpace(TargetFolder)
    Set ColItems = ObjFolder.Items
    For Each ObjItem In ColItems
        If ObjItem.Name = FileName Then
            ObjItem.InvokeVerbEx ("Print")
            Exit For
        End If
    Next
    Set ObjItem = Nothing
    Set ColItems = Nothing
    Set ObjFolder = Nothing
    Set ObjShell = Nothing
    
End Sub


Is it possible to print a PDF file without first opening it?

Thanks for any kind of advice.
 

Minty

AWF VIP
Local time
Today, 03:28
Joined
Jul 26, 2013
Messages
8,020
It appears you can multiple print up to 15 files in windows explorer without opening them, according to a google search.

I'm not sure how you would achieve this in VBA though.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 21:28
Joined
Feb 28, 2001
Messages
19,040
I have some of my ancestry files that sometimes have to be printed, my method is to browse to the right folder, then right-click on the pdf and select the print option. Even doing that manually, I get a brief flash of the PDF opening, lasting less than a full second. I have not seen a successful case of printing the PDF without opening it.

SUPPLEMENTAL EDIT:

Look at this link and perhaps instead of using VBA, consider doing a command shell to use the /h/t options to separately launch independent short-lived instances of Adobe Reader. No guarantees on this one.

 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 03:28
Joined
Sep 21, 2011
Messages
7,733
@Tera,

Were you using something like this?, you mentioned ShellExecute ?
Code:
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(hWndAccessApp, "print", strPathAndFilename, vbNullString, vbNullString, 0)

End Sub

as I do not see any file actually opening with this code.?
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 14:28
Joined
Jan 20, 2009
Messages
12,227
Files would surely have to be opened by their applications to be able to be printed otherwise the data cannot be interpreted and rendered.

Printing without opening would be running the application window without Visibility.
 

deletedT

Guest
Local time
Today, 03:28
Joined
Feb 2, 2019
Messages
1,220
@Tera,

Were you using something like this?, you mentioned ShellExecute ?
Code:
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(hWndAccessApp, "print", strPathAndFilename, vbNullString, vbNullString, 0)

End Sub

as I do not see any file actually opening with this code.?




No, but something similar. My code is:
SQL:
Public Const SW_SHOWNORMAL = 1

Public Declare PtrSafe Function ShellExecute 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 LongPtr) _
    As LongPtr

x = ShellExecute(FormName, "Print", thisFile, vbNullString, 0&, SW_SHOWNORMAL)


I tried your code. And it's perfect. I printed 50 files and not even one instance of Adobe was opened.
You're a life saver.

Million thanks.
 
Last edited:

deletedT

Guest
Local time
Today, 03:28
Joined
Feb 2, 2019
Messages
1,220
Files would surely have to be opened by their applications to be able to be printed otherwise the data cannot be interpreted and rendered.

Printing without opening would be running the application window without Visibility.

@Galaxiom Our CAD application can print the drawing files without being launched. We simply right click a *.drw file and select print. It's printed without the application being launched. I'm not sure about how it works. Maybe the application is launched and stays in the background. But even that would be strange. The CAD application is a huge one and it takes almost 2 minutes to be opened. But when we right click and select print, it's printed instantly.

The Code @Gasman submitted, prints pdf files without launching adobe. (at least for me.)

Thanks for your input.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 21:28
Joined
Feb 28, 2001
Messages
19,040
The article I referenced suggested that the /h option was "hidden" meaning it wouldn't be visible. But I didn't try it.
 

deletedT

Guest
Local time
Today, 03:28
Joined
Feb 2, 2019
Messages
1,220
The article I referenced suggested that the /h option was "hidden" meaning it wouldn't be visible. But I didn't try it.
I don't know why, but a blank pdf window pop out. Maybe a mistake on my side.
I'll give it another try.

Thanks.
 

Gasman

Enthusiastic Amateur
Local time
Today, 03:28
Joined
Sep 21, 2011
Messages
7,733
No, but something similar. My code is:
SQL:
Public Const SW_SHOWNORMAL = 1

Public Declare PtrSafe Function ShellExecute 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 LongPtr) _
    As LongPtr

x = ShellExecute(FormName, "Print", thisFile, vbNullString, 0&, SW_SHOWNORMAL)


I tried your code. And it's perfect. I printed 50 files and not even one instance of Adobe was opened.
You're a life saver.

Million thanks.
Well TBH I'd agree with Galaxiom, as I was wondering how the file would be rendered.
However as long as you do not see the app opening all the time, that should still be worthwhile.

Be aware, that if you ever want to use same in Excel, replace hWndAccessApp with hWnd
 

Users who are viewing this thread

Top Bottom