VB6 OLE Automation using IE to print HTML document without user intervention...

Summary of needs:
1 - print an HTML document without opening a window and making the user click File -> print...
2 - would like to have the option of bypassing or showing the printer dialog window.
3 - would like to have this happen "in the background", like it does when you right-click on an HTML file and select print from the menu (without IE windows opening and closing).

Goals 2 and 3 are not a requirement, but would be nice to have.  I am trying to use OLE Automation, but it doesn't have be this way.  If you have a better method of accoplishing these tasks, please let me know (maybe "rundll32" with a shell command?).

I can't get goal 1 to work using OLE Automation. I am trying to do this using VB 6 and OLE Automation with the InternetExplorer.Application object.  I found some documentation on MSDN (http://msdn.microsoft.com/workshop/browser/webbrowser/reference/objects/internetexplorer.asp).  From what I read, it tells me that I can get an Document object then get a Window object, then use the print() method.  It doesn't work, and it seems to be the only method in the Window object that doesn't.  The following is a listing of the simple code:

<code>
On Error GoTo openDoc_Err
    Dim IE As Object, win As Object
   
    Set IE = CreateObject("InternetExplorer.Application")  'create the object
   
    IE.Visible = True
    IE.Navigate "http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_window.asp"
    While IE.Busy = True
        DoEvents
    Wend
   
    Set win = IE.Document.parentwindow
    win.Print
   
    While IE.Busy = True
        DoEvents
    Wend
   
    MsgBox "done!"
   
 
openDoc_Exit:
    Screen.MousePointer = vbNormal
    Set IE = Nothing
    Set win = Nothing
    Exit Sub

openDoc_Err:
    MsgBox "ERROR -" + CStr(Err.Number) + ": " + Err.Description
    Resume openDoc_Exit

</code>

The URL in the code should take you directly to the page that lists the methods for the Window object.  I must be mis-reading.

Thanks!

kkennedyAsked:
Who is Participating?
 
flavoConnect With a Mentor Commented:
Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
 Dim IE As Object, win As Object
   
    Set IE = CreateObject("InternetExplorer.Application")  'create the object
   
    IE.Visible = True
    IE.Navigate "http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_window.asp"
    While IE.Busy = True
        DoEvents
    Wend
   
   IE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

    While IE.Busy = True
        DoEvents
    Wend
   
    MsgBox "done!"

   IE.quit
  set IE = nothing

As i side note, i think i had problems with closing IE before it printed, i dont think IE.Busy works for printing... i cant really remember...

Dave
0
 
flavoCommented:
to print without user intervention use


Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_DONTPROMPTUSER = 2

  oIE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER


0
 
flavoCommented:
to not show IE (ie poping up), just dont use oIE.Visable = true.  be sure to close it with oIe.QUIT when done
0
 
kkennedyAuthor Commented:
Thanks Dave, you get the points!

Where do I find documentation on this?  I found some documentation, but I can't seem to find how you knew that the command id was 6.  How would I find the value of other commands and options?
0
 
flavoCommented:
I cant remember sorry, i think msdn.  did it a while ago, didnt keep a list of the other ones...

Dave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.