Solved

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

Posted on 2004-08-11
5
2,339 Views
Last Modified: 2010-05-18
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!

0
Comment
Question by:kkennedy
  • 4
5 Comments
 
LVL 34

Expert Comment

by:flavo
ID: 11780570
to print without user intervention use


Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_DONTPROMPTUSER = 2

  oIE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER


0
 
LVL 34

Expert Comment

by:flavo
ID: 11780581
to not show IE (ie poping up), just dont use oIE.Visable = true.  be sure to close it with oIe.QUIT when done
0
 
LVL 34

Accepted Solution

by:
flavo earned 125 total points
ID: 11780592
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
 

Author Comment

by:kkennedy
ID: 11784290
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
 
LVL 34

Expert Comment

by:flavo
ID: 11788641
I cant remember sorry, i think msdn.  did it a while ago, didnt keep a list of the other ones...

Dave
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now