[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Printing a HTML using IE

Posted on 2005-04-25
12
Medium Priority
?
4,567 Views
Last Modified: 2010-05-18
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
Comment
Question by:srinivas_vemla
  • 8
  • 3
12 Comments
 

Author Comment

by:srinivas_vemla
ID: 13863767
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
 

Author Comment

by:srinivas_vemla
ID: 13863773
I get this message too:

dialogArguments.__IE_PrintType is null or not an object
res://C:\\WINDOWS\System32\shdoclc.dll/preview.dlg
0
 

Author Comment

by:srinivas_vemla
ID: 13872967
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:srinivas_vemla
ID: 13872977
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
 

Assisted Solution

by:ScribbleMeat
ScribbleMeat earned 300 total points
ID: 13876210
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
 
LVL 26

Accepted Solution

by:
EDDYKT earned 1200 total points
ID: 13876257
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
 

Author Comment

by:srinivas_vemla
ID: 13879589
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 13879680
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
 

Author Comment

by:srinivas_vemla
ID: 13879867
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
 

Author Comment

by:srinivas_vemla
ID: 13879962
It fails on the:

Set iSHDocIE = New SHDocVw.InternetExplorer

statement within the referenced DLL...
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 13884389
you can use webbrowser ocx on form, you still can compile as dll.

Just load the form without show
0
 

Author Comment

by:srinivas_vemla
ID: 13899569
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 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