1

I have a folder containing 1650 HTML files that, due to local authorities, must be also printed in paper form.

I have tried with classic ctrl+A (even if I was trying on smaller quantities) and then I have looked for PRINT label in the right click menu, but there's any.

If I choose to print multiple jpg or pdf files, the PRINT voice appears.

How am I supposed to print multiple html files? A batch file? (I have no knowledge about how to).

I have also thought to convert html to pdf, but had no success with PDF Creator and PDF Architect.

Any of you with some experience to share? I have wrtten some code mixing the one given by Tim and what I found on stackoverflow, but had no success.

Here it is:

Set objFSO = CreateObject("Scripting.FileSystemObject")
  objStartFolder = "C:\Users\mainUser\Desktop\ft"

  Set objFolder = objFSO.GetFolder(objStartFolder)
  Set colFiles = objFolder.Files

  For Each objFile in colFiles
  strFileName = objFile.Name

  If objFSO.GetExtensionName(strFileName) = "html" Then

        On Error Resume Next

        Const OLECMDID_PRINT = 6
        Const OLECMDEXECOPT_DONTPROMPTUSER = 2
        Const PRINT_WAITFORCOMPLETION = 2

        Dim objExplorer
        Set objExplorer = CreateObject("InternetExplorer.Application")
        objExplorer.Navigate objFolder.Path +"\"+ objFile.Name
        objExplorer.Visible = 1

        Do while objExplorer.ReadyState <> 4
            WScript.Sleep 1000 'milliseconds
        Loop

        oIExplorer.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

  End If

  Next

It just opens one html in internet explorer. I thought it would have open all the files, and already in PRINT "mode". I think I am missing something.

Skatox
  • 4,237
  • 12
  • 42
  • 47
Aptivus
  • 433
  • 1
  • 8
  • 24
  • It could be a programming question but what happens if you open Printers and drag'n'drop the selected files from the Explorer window onto the printer icon? – wOxxOm Aug 04 '15 at 16:34
  • Here's a link to "How to convert HTML to PDF" http://www.win2pdf.com/doc/index.html?html-to-pdf.htm – Happy Face Aug 04 '15 at 16:36
  • Converting file by file? It's easier to open each html file with browser and then print it. Is there the chance to make a bulk conversion of html files into pdf's? – Aptivus Aug 04 '15 at 17:30

1 Answers1

2

See this thread for looping through files in a directory: How to do something to each file in a directory with a batch script

Then you can use the native PRINT command so your batch file could be as simple as this:

for /f "delims=|" %%f in ('dir /b c:\') do PRINT %%f


In light of the comments below:


Ahh. Then you will likely have to automate IE to print the page. Play with a VBScript along these lines:

Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
Const PRINT_WAITFORCOMPLETION = 2

objStartFolder = "C:\Users\mainUser\Desktop\ft"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(objStartFolder)
Set objExplorer = CreateObject("InternetExplorer.Application")

Set oShell = CreateObject("Shell.Application")

For Each objFile In objFolder.Files
    strFileName = objFile.Name
    If objFSO.GetExtensionName(strFileName) = "html" Then
        handle = objExplorer.Hwnd
        objExplorer.Navigate objFolder.Path + "\" + objFile.Name

        For Each Wnd In oShell.Windows
            If handle = Wnd.Hwnd Then Set objExplorer = Wnd
        Next

        Do While objExplorer.Busy
            WScript.Sleep 1000 'milliseconds
        Loop

        objExplorer.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
    End If
Next
Set oShell = Nothing
Set objFSO = Nothing
Set objFolder = Nothing
Set objExplorer = Nothing

3rd Update:

I fixed the code above. I apologize.Its been some time since I've worked with IE. Apparently IE treats each tab as a new instance of IE, and loading a document "creates" a new tab. As a result, we have to load the page and then find our IE window again so we can set it back to the variable.

Community
  • 1
  • 1
Tim
  • 2,701
  • 3
  • 26
  • 47
  • You won't be happy with the output of the `print` command as it is intended to print _text_ files, so you'll print the HTML source text... – aschipfl Aug 04 '15 at 17:22
  • I have tried this, but with no success. I have changed the folder where the files are, but nothing happens. What is wrong with it? Here it is my bat file: `for /f %%f in ('dir /b C:\Users\mainUser\Desktop\ric\') do echo %%f` – Aptivus Aug 04 '15 at 17:25
  • thank @aschipfl ! No, I need to print fomratted html page indeed, not the source code of it. – Aptivus Aug 04 '15 at 17:29
  • @Tlm Thank you very much. Just one stuipd question....where am I supposed to code that snippet of yours? Where the vbscript should be placed and how to launch it? Moreover, I cannot see any recursiveness in that script. It seems to process one file at a time, doesn't it? Thank you for your patience! – Aptivus Aug 04 '15 at 18:08
  • @Aptivus, A VBScript is a .txt (text) file with a .vbs file extension. Create a new text document and paste the code in there, then change the extension to .vbs. And no, there is no looping in my code. Looping through the files in a directory is a pretty basic task: http://stackoverflow.com/questions/16665748/vbscript-to-loop-through-all-files-in-a-folder or http://stackoverflow.com/questions/4200028/vbscript-list-all-pdf-files-in-folder-and-subfolders or http://stackoverflow.com/questions/3620773/vbs-help-me-loop-this-through-a-directory – Tim Aug 04 '15 at 19:22
  • Thank you @Tim I will try it in the next few hours – Aptivus Aug 05 '15 at 13:04
  • Hello @Tim, I tried mixing some vbs, but it only opens just one html file. I am not sure I did it wright. Please see my editing in my post. Thank you very much. – Aptivus Aug 06 '15 at 18:14
  • It works! At least with 5 files at a time. 5+ Explorer crashes :( – Aptivus Aug 07 '15 at 09:01
  • Just a thought (since it crashes after 5 printings), I wonder if you could create a 2 part process to speed this up. The first part being a batch file that would loop through the files in the directory and pass each file to a VBScript (the 2nd part) that would then print the passed file. It might be much faster, and it would essentially be a multi-threaded app (since each new instance of the printing script would/should be a new thread). **However** it could potentially kill you printer and/or print spool since that is most likely your biggest bottleneck. – Tim Aug 07 '15 at 13:19
  • One more thought: If these files get created a semi-regularly, you could modify the script to copy them to a "Completed" folder after the page is printed. Then set the script up as a scheduled task that runs once a week/month. Then all you have to do is pick them up at the printer. (I work hard to be lazy) – Tim Aug 07 '15 at 13:21
  • Nice thoughts indeed! Probelm is, as you may have noticed, I am no expert at VBS. As to your second thought, luckily this process of mass printing will be no more needed. I had some mods on the system, so it now generates a big pdf that can be sent to printer once a month, printing no more than 150 pages per time. – Aptivus Aug 07 '15 at 15:24