Printing html file to a specific printer

Juice

New member
Local time
Today, 21:56
Joined
Jan 20, 2025
Messages
6
Hi everyone!

Here are the basics: I have an .html file that i want to print to pdf.

Specs: Win 10, 2003 office

The code below is what I try to use to print, but it always gives me error 31. From what i understand error 31 means windows dont have a designated app for .html. I checked and it does so iam looking for alternatives, preferable not third party solutions. Also i tested the code with a pdf file and works that way. Also set chrome, firefox and edge too to be the designated app to .html, but no luck.

Code:
ShellExecute(0, "Print", "c:\1\test.html", 0, 0, SW_HIDE)

Now for the long part:

I have an automation that uses word templates to generate letters and print it to pdf to be sent out to customers. I generate roughly a 100 a day, and one is about 13-15 seconds to make. Since reports dont have the the normal kind of justify and other featuers like make one part of the text bold and such (basicly its not preaty for the company) , word templates was the solution.

Since DDE is not been usable for a while and behind the older querys (started with office 97), i got a lot of code, i was looking for other solutions and i had 2 ideas. Also i planing to go higher on the office version, just need to clear out this kind of problems (macros, code in query, etc), since microsoft hell bent on security.

The first one was to run a query to make a table and wire that table to a word template, merge it and then separate to pdf files and make an automation to file it in the system and send it out. Thats done but i have like 10+ different templates and its still a pain in the ass, because it nedds a lot of separate steps for each letter template. Some templates only needs basic customer data, but some have concatenadet stuff, calculations

So my fantasticly dumb ide was to make bot that runs trough a task list, figure out which kind of letter is needed based on the task name, generate an html and make a pdf and file it. It runs realy fast (like a second), but cant make a pdf...
 
Error 31 is Application-defined or object-defined error
If you right click the html file, does it show Print as an option?

Edit: ChatGPT suggests forcing an app to do the print
Code:
ShellExecute 0, "print", "C:\Program Files\Internet Explorer\iexplore.exe", _
              """C:\Path\To\Your\File.html""", vbNullString, 0

Also
Not all applications support the "print" verb via ShellExecute.
I cannot get IE to print, though I do not get any error.
 
Last edited:
Error 31 is Application-defined or object-defined error
If you right click the html file, does it show Print as an option?

Edit: ChatGPT suggests forcing an app to do the print
Code:
ShellExecute 0, "print", "C:\Program Files\Internet Explorer\iexplore.exe", _
              """C:\Path\To\Your\File.html""", vbNullString, 0

Also

I cannot get IE to print, though I do not get any error.

Your code is attempting to use ShellExecute to print an HTML file via Internet Explorer, but Internet Explorer does not directly support the "print" verb in this context. This approach often fails silently without errors.
Here are a few alternative methods you might try:

Option 1: Use Internet Explorer's COM Object
Instead of ShellExecute, you can use the Internet Explorer COM object to load and print the file:

Code:
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.Navigate "C:\Path\To\Your\File.html"

Do While ie.ReadyState <> 4
    DoEvents
Loop

ie.ExecWB 6, 1  ' 6 is for Print, 1 is for default options
ie.Quit
Set ie = Nothing

This method launches Internet Explorer, loads the HTML file, and then prints it using ExecWB.

Option 2: Use the Default Browser Instead

You might want to print using the default browser instead of relying on Internet

Code:
Dim objShell
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "C:\Path\To\Your\File.html", "", "", "print", 1
Set objShell = Nothing
Explorer:

This executes the "print" verb on the file, letting the system determine the appropriate application.

Option 3: Convert to PDF First

If printing an HTML file directly is unreliable, consider converting it to PDF using tools like wkhtmltopdf or Microsoft Print to PDF.
 

Users who are viewing this thread

Back
Top Bottom