Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Set Default Printer using PrintDialog.PrinterSettings

Avatar of Jeanette Durham
Jeanette DurhamFlag for United States of America asked on
Visual Basic.NET
9 Comments1 Solution2560 ViewsLast Modified:
Dear Experts,

I am trying to make one of my vb.net 2010 programs work. (I am using IE 8). Basically, for awhile I was doing it this way:

    Public Function PrintMe_SHDocVWMethod(byval firstTime as boolean) as boolean
        Const PRINT_WAITFORCOMPLETION = CShort(2)   'use this setting to tell ExecWB to wait to return
        Try
            Dim eQuery As SHDocVw.OLECMDF
            Me.myWebBrowser
            eQuery = Me.myWebBrowser.QueryStatusWB(SHDocVw.OLECMDID.OLECMDID_PRINT)
            If Err.Number = 0 Then
                If eQuery And SHDocVw.OLECMDF.OLECMDF_ENABLED Then
                    Select Case firstTime
                        Case True
                            Me.myWebBrowser.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER, PRINT_WAITFORCOMPLETION, 0)
                            Return True
                        Case False
                            Me.myWebBrowser.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, PRINT_WAITFORCOMPLETION, 0)
                            Return True
                    End Select
                Else
                    MsgBox("The Print command is currently disabled. Please enable it in your browser and try again.")
                    Return False
                End If
            End If
        Catch ex As Exception
            MsgBox("Unknown error! Print failed!" & vbCrLf & ex.Message)
        End Try
    End Function

But when the program was in full screen and I tried to show the print dialog it wouldn't come up. I believe this is because the IE I was automating was in the background and not visible. It would come up but the only way you could get to the dialog or see the dialog was control-tabbing to it, which is just not going to work.

So I tried adding an actual webbrowser control to my form but the webbrowser control doesn't have the ExecWB method.

So then I'm looking at it and it's got the ShowPrintDialog() method. So I try that, and it doesn't show a true dialog.. what actually happens is the print dialog pops up and the code keeps executing and I could do some api calls probably to find out when the user actually clicks ok or cancel and watch for the window vanish. But even then, I can't tell if the user cancelled the print job or not. I just have to assume they hit 'print'.

So then I'm like, ok, if I add a printDialog to the form and show that, the user can pick whatever settings he wants including the printer and then hit print or cancel, and I can test the dialogresult to find out. Yay! But.. when I tried that none of the settings I picked (including the printer I chose) were used. The printDialog box had nothing to do with the webbrowser's printer settings. So when I went myWebBrowser.print() it just used whatever the windows printer was.

So basically, I want to show a dialog to the user and I need to make the settings they choose be the settings that are used, whether by actually setting the default printer to the printdialog.printersettings (is this possible? I'd like to do this) or by somehow using my old original code but on the webbrowser control on my form. (this would be even better)

Here is my current non-working code..

Public Function PrintMe_WebBrowserMethod(ByVal firstTime As Boolean) As Boolean
        'Returns false if user aborted or if there was an error
        Try
            Select Case firstTime
                Case True
                    Me.myWebBrowser.ShowPrintDialog()

                    'Ask user for printer settings and confirm that he clicks 'Print'
                    Dim diagResult As System.Windows.Forms.DialogResult = Me.PrintDialog1.ShowDialog()
                    If diagResult <> System.Windows.Forms.DialogResult.OK Then
                        Return False
                    End If
                    Me.myWebBrowser.Print()
                Case (False)
                    Me.myWebBrowser.Print()
            End Select
        Catch ex As Exception
            MsgBox("Print failed! on PrintMe_WebBrowserMethod()" & vbCrLf & ex.Message)
        End Try
    End Function

Thanks! ~Jeffrey
ASKER CERTIFIED SOLUTION
Avatar of puppydogbuddy
puppydogbuddy

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answers