Solved

How do I print?

Posted on 2004-09-05
11
490 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

758 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

23 Experts available now in Live!

Get 1:1 Help Now