Solved

How do I print?

Posted on 2004-09-05
11
496 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

803 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