Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a HTML file and I have to give print to printer

From IExplore we choose File menu and give Print, the same thing I want in Win32::OLE

Please help me that how to set print.

Replies are listed 'Best First'.
Re: how to print through Win32::Ole
by holli (Abbot) on Feb 24, 2005 at 10:54 UTC
    this link might be interesting for you, the code examples given there are in VBScript, but it is straighforward to translate them to perl.

    Because nothing is as old as the link from yesterday, here is a little excerpt:

    Printing HTML

    Like many scripters, I often dump report data as HTML-formatted text. Although you can easily use a Microsoft Internet Explorer (IE) automation object to print an HTML file, IE 5.0 and later always display a print dialogue box because Microsoft considers printing through IE to be a security concern. For a detailed discussion of this concern, see the Microsoft article "Printing with the Internet Explorer WebBrowser Control"

    One way to print HTML files from a script is to use an external component, such as MeadCo's ScriptX. A subset of ScriptX's printing functionality is available at http://www.meadroid.com. I don't discuss the use of ScriptX in this article, but you can download it and play with it.

    Another approach to programmatically printing HTML files is to use Microsoft Office. You can use Office two ways. The obscure approach is to use msohtmed.exe, a little-used program that typically resides in the C:\program files\microsoft office\office11 folder (for Office System 2003) or in the \office 10 folder (for Office 2000). You call msohtmed.exe as you do Notepad. For example, to print a file called MyLog.htm, you'd use the code
    "C:\Program Files\Microsoft Office\Office11\msohtmed.exe" /p "C:\MyLog.htm"
    A more common solution is to automate Microsoft Word. Automating Word is easy, and Word can open, read, and print text files. Listing 5 shows how to print from Word. The PRNT_BACKGROUND constant is set to False to ensure that printing occurs in the foreground so that the script doesn't continue to execute until after printing finishes. The DONT_SAVE constant has a value of False to ensure that Word doesn't overwrite the HTML document upon closing it.


    holli, /regexed monk/
Re: how to print through Win32::Ole
by Sec (Monk) on Feb 24, 2005 at 11:06 UTC
    I'm no Win32::OLE guru. From what i gleaned from the documentation this should do the trick:
    use Win32::OLE; my $ie = Win32::OLE->new("InternetExplorer.Application") or die "oops: ".Win32::OLE::LastError(); $ie->{'Visible'}=1; $ie->Navigate("http://www.perlmonks.org/"); sleep(1); $ie->ExecWB(6,0);
    If you use $ie->ExecWB(6,2); it should print without popping up the print preferences dialog (not tested, because i don't have a printer right now)