Solved

How do I print?

Posted on 2004-09-05
11
495 Views
Last Modified: 2013-12-25
Here's my code:

Private Sub Command1_Click()
    WebBrowser1.Navigate "www.cnn.com"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, 0, 0
End Sub

Private Sub WebBrowser1_StatusTextChange(ByVal Text As String)
    Me.Caption = Text
End Sub

I get the following error message with the "WebBrowser1.ExecWB" line highlighted in yellow:
Run-time error '-2147721248 (80040100)':
Method 'ExecWB' of object 'IWebBroser2' failed

Why?
0
Comment
Question by:shaolinfunk
  • 6
  • 3
  • 2
11 Comments
 
LVL 20

Expert Comment

by:hes
ID: 11984593
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 11984676
Yeah MSDN was the first thing I checked.  But,  I didn't use any of the OLECMDID values they mentioned so I still don't understand how to get rid of the error.  Can you show me how?
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11984731
Do you have Option Explicit at the top of the form?
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 11984889
I did not.

But I added the line "Option Explicit" to the top of the form
and still geot the same error.
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11985047
I tried your code on my machine and it works ok. I get the print dialog up as you would expect.
Do you get this far?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Expert Comment

by:hes
ID: 11985319
Have you tried this (from msdn)

Dim eQuery As OLECMDF       'return value type for QueryStatusWB

      On Error Resume Next
      eQuery = WebBrowser1.QueryStatusWB(OLECMDID_PRINT)  'get print command status
      If Err.Number = 0 Then
                If eQuery And OLECMDF_ENABLED Then
                    WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, "", ""    'Ok to Print?
              Else
                    MsgBox "The Print command is currently disabled."
                End If
      End If
      If Err.Number <> 0 Then MsgBox "Print command Error: " & Err.Description
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 11985328
No.

Try saving and closing your project.  Then close your VB editor.
Then re-open the VB Editor and re-open the project.  Then do Ctrl + F5 to "Start with Full Compile".  Did the app still run without any errors?
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 11985334
Hi hes,

I pasted your code this way and got a "permission denied" error..

Private Sub Command1_Click()
    WebBrowser1.Navigate "www.cnn.com"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    Dim eQuery As OLECMDF       'return value type for QueryStatusWB

     On Error Resume Next
     eQuery = WebBrowser1.QueryStatusWB(OLECMDID_PRINT)  'get print command status
     If Err.Number = 0 Then
              If eQuery And OLECMDF_ENABLED Then
                  WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, "", ""    'Ok to Print?
            Else
                  MsgBox "The Print command is currently disabled."
              End If
     End If
     If Err.Number <> 0 Then MsgBox "Print command Error: " & Err.Description

End Sub

Private Sub WebBrowser1_StatusTextChange(ByVal Text As String)
    Me.Caption = Text
End Sub
0
 
LVL 20

Accepted Solution

by:
hes earned 75 total points
ID: 11985392
Ok your code did work for me until I tried your suggestion of saving then reopening and Ctrl + F5 , Got the same error.
Add this

Private Sub Form_Load()
  WebBrowser1.Stop
End Sub

The webbrowser control in compiled mode tries to access the internet at form load complete, thus the error, no web page to print.
I also changed this because I was gettint about 6 printer dialog boxes displayed

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    If WebBrowser1.ReadyState = READYSTATE_COMPLETE Then
      WebBrowser1.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, 0, 0
    End If
End Sub
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 11985692
Thanks for explaining why it wasn't working.  The .Stop code solves the problem, no more errors.  
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 11985838
Is there a way to set WebBrowser1.Visible = False and still have this code work i.e. still print?


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now