4

I am using ShellExecute through C and that seem's work OK except one issue.

ShellExecute(NULL, "print", "C:\\index.html", NULL, NULL, SW_HIDE);

With this I would like to get print dialog for press OK for start printing but instead of that MS Word opens with file C:\index.html loaded.

How to get print functionality on html files with ShellExecute?

If is important to note, IE is not my default internet browser.

Wine Too
  • 4,515
  • 22
  • 83
  • 137

1 Answers1

7

You are relying on the shell's associations to print the file, but that's a terribly brittle approach. If you right click on the file and select Print you'll observe the same behaviour as your call to ShellExecute.

So, if you want to use ShellExecute with the Print verb you will need to change your machine's configuration. You need to make sure that the machine's associations are configured to handle the Print verb on a .html file in a way that suits you. You could do that for your machine but you cannot expect to do it for other people's machines.

Instead you could run this command to be sure that the HTML file will be printed:

rundll32.exe %windir%\system32\mshtml.dll,PrintHTML "C:\index.html"

You can translate that readily into a ShellExecute call.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • That sound's very good. Please help in translation to ShellExecute. My approach: ShellExecute(NULL, "open", "rundll32.exe %windir%\system32\mshtml.dll,PrintHTML", myfile, NULL, SW_NORMAL); seems don't work at all. – Wine Too May 17 '13 at 09:56
  • You'll need to substitute the actual system directory because `ShellExecute` won't expand the path for you. And it seems that the filename needs to be quoted. So, this will work: `ShellExecute(0, NULL, "rundll32.exe", "C:\\windows\\system32\\mshtml.dll,PrintHTML \"C:\\index.html\"", NULL, SW_NORMAL);` – David Heffernan May 17 '13 at 10:09
  • Thank you David, works excellent, just as expected! sprintf(sfile, "%s%s%s%s", "mshtml.dll,PrintHTML \"", cwd, afile, "\""); ShellExecute(NULL, "open", "rundll32.exe", sfile, NULL, SW_SHOWNORMAL); – Wine Too May 17 '13 at 10:17
  • Is here any chance to get popped dialog in center of screen? – Wine Too May 17 '13 at 10:56
  • Well, not easily. What you would need to do would be install a CBT_HOOK, and use that to detect that the dialog was about to show. And then you could re-position it. – David Heffernan May 17 '13 at 11:06
  • OK, not so important to do that. Thanks. – Wine Too May 17 '13 at 11:12
  • I noticed that the CSS interpretation while rendering the HTML using this approach is different than when using Internet Explorer directly (same html/css files). IS this mshtml dll maintained and synced with the IE releases or is it a different library? – wirrbel Oct 29 '13 at 14:22
  • @wirrbel It will be the same library, but it will be running in a legacy compat mode. You'd need to opt in to modern rendering with registry setting and doctype, I guess. – David Heffernan Oct 29 '13 at 14:31