• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4768
  • Last Modified:

Printing a HTML using IE

Hello Friends,

I use the following code to print HTML files using the SHDOCVW.DLL:

Dim iSHDocIE As SHDocVw.InternetExplorer
Dim iSHDocWebBrowser As SHDocVw.WebBrowser

Set iSHDocIE = New SHDocVw.InternetExplorer
Set iSHDocWebBrowser = New SHDocVw.WebBrowser

Set iSHDocWebBrowser = iSHDocIE
iSHDocWebBrowser.Visible = False      'to enable background printing

iSHDocIE.navigate "C:\Test.html"
iSHDocIE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

It worked fine the first time (I sent it to a network printer and it printed without any errors). Now I get the following error:
Run-time error -2147221248
Method 'ExecWB' of object 'IWebBrowser2' failed

And when I click Debug and then continue, it prints. But I don't want this user interaction.

Can anyone tell me what is wrong with this code? A similar code in VB.Net always works like a charm for me.

Regards,
Srinivas
0
srinivas_vemla
Asked:
srinivas_vemla
  • 8
  • 3
2 Solutions
 
srinivas_vemlaAuthor Commented:
I have changed my code to the following:

Dim iSHDocIE As SHDocVw.InternetExplorer
Dim iSHDocWebBrowser As SHDocVw.WebBrowser

Set iSHDocIE = New SHDocVw.InternetExplorer
Set iSHDocWebBrowser = New SHDocVw.WebBrowser

Set iSHDocWebBrowser = iSHDocIE
iSHDocWebBrowser.Visible = False     'to enable background printing

iSHDocIE.Navigate "C:\Temp.htm"

Retry:
If (iSHDocIE.QueryStatusWB(OLECMDID_PRINT) <> 0) Then
    Debug.Print "YES"
    Debug.Print iSHDocIE.QueryStatusWB(OLECMDID_PRINT)
    iSHDocIE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
Else
    Debug.Print "NO"
    Debug.Print iSHDocIE.QueryStatusWB(OLECMDID_PRINT)
    GoTo Retry
End If


But not much result. I was thinking that if the file is loaded, then it should be able to be printed. But it prints only if I put a break point somewhere in the code and step thru... If I just let it run, it does nothing.

Any suggestions plssss...

Thanks,
Srinivas
0
 
srinivas_vemlaAuthor Commented:
I get this message too:

dialogArguments.__IE_PrintType is null or not an object
res://C:\\WINDOWS\System32\shdoclc.dll/preview.dlg
0
 
srinivas_vemlaAuthor Commented:
Finally I get this working as:


Dim WithEvents iSHDocIE As SHDocVw.InternetExplorer
Dim WithEvents iSHDocWebBrowser As SHDocVw.WebBrowser

Private Sub Command1_Click()
Set iSHDocIE = New SHDocVw.InternetExplorer
Set iSHDocWebBrowser = New SHDocVw.WebBrowser

Set iSHDocWebBrowser = iSHDocIE
iSHDocWebBrowser.Visible = False     'to enable background printing

iSHDocIE.Navigate "C:\Temp.htm"
End Sub

Private Sub iSHDocIE_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
Retry:
If (iSHDocIE.QueryStatusWB(OLECMDID_PRINT) <> 0) Then
    Debug.Print "YES"
    Debug.Print iSHDocIE.QueryStatusWB(OLECMDID_PRINT)
    iSHDocIE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
Else
    Debug.Print "NO"
    Debug.Print iSHDocIE.QueryStatusWB(OLECMDID_PRINT)
    GoTo Retry
End If
End Sub

But when I use the same code to make a DLL and reference it somewhere else, i get this error:

dialogArguments.__IE_PrintType is null or not an object

This is getting me crazy... does anyone have any better ideas than this code??? something actually working????
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
srinivas_vemlaAuthor Commented:
As a contn my previous post, the requirement is to have a code that prints HTML files using Internet Explorer object. This printing should be done without any user interaction... and the code shud be make'able into a DLL...

Please do reply if you have worked on this ever...
0
 
ScribbleMeatCommented:
As far as I know, many of the WebBrowser's controls are disabled if the .Visible is set to false. MS did this (IMHO) to protect the end user from sneaky developers.

Instead of setting .Visible to false, try hiding it some other way. You could move the control out of the visible area your form, or place it in a container that is obscured by another container.

Hope this helps,

-- ScribbleMeat
0
 
EDDYKTCommented:
I use webbrowser control instead and this works for me

Option Explicit

Private Sub Form_Load()
Timer1.Interval = 1
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
Timer1.Enabled = False
WebBrowser1.Navigate Command
End Sub

Private Sub Timer2_Timer()
Timer2.Enabled = False
End
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Debug.Print pDisp
If (pDisp = WebBrowser1) Then
WebBrowser1.ExecWB 6, 2, 0, 0
Timer2.Interval = 3000
Timer2.Enabled = True
End If
End Sub
0
 
srinivas_vemlaAuthor Commented:
Hello EddyKT,

As suggested by you, I tried with the following code:

Dim WithEvents iSHDocWB As SHDocVw.WebBrowser

Private Sub Command1_Click()
    Set iSHDocWB = New SHDocVw.WebBrowser
    iSHDocWB.Navigate "C:\Documents and Settings\PN07934\Desktop\Transform\AutoTestC.html"
End Sub

Private Sub iSHDocWB_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    iSHDocWB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End Sub

But the error props up saying:
Run-time error '-2147477259 (80004005)'
Automation error
Unspecified error

Any ideas???

0
 
EDDYKTCommented:
do this


Private Sub iSHDocWB_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp = iSHDocWB) Then
    iSHDocWB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
end if
End Sub
0
 
srinivas_vemlaAuthor Commented:
That doesn't work either, but this works:

Dim WithEvents iSHDocWB As SHDocVw.WebBrowser
Dim WithEvents iSHDocIE As SHDocVw.InternetExplorer

Private Sub Command1_Click()
    Set iSHDocIE = New SHDocVw.InternetExplorer
    Set iSHDocWB = iSHDocIE
    iSHDocWB.Navigate "C:\Documents and Settings\PN07934\Desktop\Transform\AutoTestC.html"
End Sub

Private Sub iSHDocWB_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    If (pDisp = iSHDocWB) Then
        iSHDocWB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
    End If
End Sub

Again, if I develop an Activex DLL as:

Dim WithEvents iSHDocIE As SHDocVw.InternetExplorer
Dim WithEvents iSHDocWB As SHDocVw.WebBrowser

Public Function PrintPDF(FileName As String) As Integer
    Set iSHDocIE = New SHDocVw.InternetExplorer
    Set iSHDocWB = iSHDocIE
   
    iSHDocWB.Navigate FileName
End Function

Private Sub iSHDocIE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    iSHDocWB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End Sub

And use it as:

Private Sub Command1_Click()
    Dim pdfW As PDFWriter.PDFWriterClass
    Set pdfW = New PDFWriterClass
   
    pdfW.PrintPDF ("C:\Documents and Settings\PN07934\Desktop\Transform\AutoTestC.html")
End Sub

I get the "dialogArguments.__IE_PrintType" error...

Any ideas???
0
 
srinivas_vemlaAuthor Commented:
It fails on the:

Set iSHDocIE = New SHDocVw.InternetExplorer

statement within the referenced DLL...
0
 
EDDYKTCommented:
you can use webbrowser ocx on form, you still can compile as dll.

Just load the form without show
0
 
srinivas_vemlaAuthor Commented:
I finally used this:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As
Long)
 
 Public Sub PrintPDF(fileName As String)
     Set ieObj = CreateObject("InternetExplorer.Application")
     ieObj.Visible = True
     ieObj.Navigate fileName
     ieObj.ExecWB 6, 2
     Sleep 1000
     ieObj.Quit
     Set ieObj = Nothing
 End Sub

This worked even after being compiled into a DLL.

Thanks guys for your replies... they were very helpful and guiding...
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now