Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing to a specific printer with MSHTML

29 views
Skip to first unread message

Michael Jervis

unread,
Jun 27, 2005, 3:15:29 PM6/27/05
to
Hi,

I need to print HTML documents without any user intervention, to a
variety of printers.

To this end I have been looking at using the IOleCommandTarget::exec
method with IDM_PRINT on an MSHTML object. I've encountered numerous
hurdles en-route, broken links in the MSDN, changed API's without
updates etc.

Firstly, IDM_PRINT can't use a custom print template from VB any more
for security reasons, so I am stuck having to do it in C++, and, I am
not a C++ or even a C programmer, so I'm getting no-where fast.

Further, it appears that the registry key the docs state that the
browser uses to select the printer
HKCU/Software/Microsoft/InternetExplorer/PageSetup/printer is actually
no longer used as of IE6 either.

So I have two problems I need help with if anyone has the time and
information:

1) How do I create an instance of a web browser OFFSCREEN. all the code
I can find shows me how to display it in a form. This is to be in a
windows service, and so can't render. All the MFC Form display stuff is
useless and thus far I have been unable to create a non-rendered one
and get Navigate to work correctly.

2) How can I persuade IE6 to print to a specific printer? Toggling the
registry doesn't work as IE6 ignores it, is there an undocumented API?
Or is there a nice way to toggle the default printer for the current
user to the one I intend to use? Failing that, how would I go about
manipulating the print dialog programatically?

I've seen ScriptX from Meadriod, it's not an option for various
reasons, neither are any of the other commercial products I've seen and
I've yet to find an open source or freeware alternative.

Thanks in advance for any help,

Michael Jervis

Michael Jervis

unread,
Jun 28, 2005, 5:16:09 AM6/28/05
to
The following code fails, the HRESULT from navigate is the good old
"unknown error code". Could anyone suggest where I'm going wrong?

IWebBrowser2 *pDoc = NULL;

CoInitialize(NULL);

CoCreateInstance(CLSID_WebBrowser,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWebBrowser2,
(LPVOID *) &pDoc);
if (pDoc)
{
HRESULT hr2 = pDoc->Navigate(L"c:\\test.html", &noarg, &noarg, &noarg,
&noarg);
}

Igor Tandetnik

unread,
Jun 28, 2005, 8:52:02 AM6/28/05
to
"Michael Jervis" <mje...@gmail.com> wrote in message
news:1119950169....@f14g2000cwb.googlegroups.com

> The following code fails, the HRESULT from navigate is the good old
> "unknown error code". Could anyone suggest where I'm going wrong?

Most ActiveX controls, WebBrowser included, need to be properly hosted
before they can work. You cannot just create one as you would a simple
non-visual COM object, and expect it to work.

Besides, the first parameter to Navigate is a BSTR that has to be
allocated with SysAllocString.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Michael Jervis

unread,
Jun 28, 2005, 10:35:44 AM6/28/05
to
Well,

It's possible to create it in Visual Basic, without displaying it in an
application, so there must be a way to set up the proper hosting for
one in C++ without displaying an IE browser window, I just can't figure
out what it would be.

I'm looking for help in that direction, do you know of any sample code
or easy tutorial I could follow?

Cheers,

Mike

Dennis Patterson

unread,
Aug 3, 2005, 10:26:52 PM8/3/05
to
If you iterate through the available printers programatically, could you
ID the printer(s) you want to write to?
Are you familiar with the DocumentProperties, DeviceCapabilities API's
in the printing API set?
0 new messages