Link to home
Start Free TrialLog in
Avatar of srinivas_vemla
srinivas_vemla

asked on

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
Avatar of srinivas_vemla
srinivas_vemla

ASKER

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
I get this message too:

dialogArguments.__IE_PrintType is null or not an object
res://C:\\WINDOWS\System32\shdoclc.dll/preview.dlg
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????
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...
SOLUTION
Avatar of ScribbleMeat
ScribbleMeat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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???

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
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???
It fails on the:

Set iSHDocIE = New SHDocVw.InternetExplorer

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

Just load the form without show
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...