Solved

How do I print?

Posted on 2004-09-05
11
498 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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
 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 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