Solved

printing a html file from a vb.net application

Posted on 2010-08-24
8
322 Views
Last Modified: 2012-05-10
Hi,

Say I have an html file at c:somefile.html and I want to print it.

I dont want to display it at all.

Can someone get me going on this as is an urgent requirement.

Thanks
0
Comment
Question by:scm0sml
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 20

Expert Comment

by:alainbryden
ID: 33510629
http://www.dotnet4all.com/Dot-Net-Books/2004/09/printing-webpages-with-vbnet.html

1. Add Reference - Microsoft.mshtml
2. Open a form and add in an invisible com object named: Microsoft Web Browser

--
Alain
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
        Dim sPrintEm As Integer = 1
        Dim eQuery As SHDocVw.OLECMDF

        AxWebBrowser1.Navigate("http://www.google.com")

        eQuery = AxWebBrowser1.QueryStatusWB(SHDocVw.OLECMDID.OLECMDID_PRINT)
        If Err.Number = 0 Then
            If eQuery And SHDocVw.OLECMDF.OLECMDF_ENABLED Then
                Select Case sPrintEm
                    Case "1"
                       
AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER, 0, 0)
                    Case "2"
                       
AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, 0, 0)
                End Select
            Else
                MsgBox("The Print command is currently disabled.")
            End If
        End If
    End Sub

Open in new window

0
 
LVL 9

Accepted Solution

by:
wellhole earned 500 total points
ID: 33510743
You are going way out of your way doing it that way in vb.net. Try this instead.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim browser As New WebBrowser
    browser.Navigate("http://google.com")
    AddHandler browser.Navigated, AddressOf com
  End Sub

  Private Sub com(ByVal sender As Object, ByVal e As EventArgs)
    With CType(sender, WebBrowser)
      RemoveHandler .Navigated, AddressOf com
      .Print()
    End With
  End Sub

Open in new window

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33511156
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:scm0sml
ID: 33511349
wellhole that looks spot on.

I haven't got a printer set up on the machine I am on at the moment but it printed to pdf etc.

Would you expect that to prompt me as I don't really want it to? If so can I stop it?
0
 
LVL 9

Expert Comment

by:wellhole
ID: 33511431
Since you're printing to pdf, your PDF printer is prompting you not your application. If your default printer is set to a normal printer, it will not prompt.
0
 
LVL 28

Expert Comment

by:Ark
ID: 33517328
OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER means webbrowser will show standard print dialog where you can choose printer, printing area, copies etc. To skip this dialog and use default printer/settings use OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER
0
 

Author Comment

by:scm0sml
ID: 33518985
ok good stuff.

my last question is the page 1 of 1 etc and the file name at the bottom of the page.

Do I have to use the ie settings to stop this or can i do it from my code?
0
 
LVL 28

Expert Comment

by:Ark
ID: 33527864
   Public Class IE_PageSettings
        Public header As String
        Public footer As String
        Public Sub New(ByVal _header As String, ByVal _footer As String)
            header = _header
            footer = _footer
        End Sub
    End Class

    Public Property IEPageSettings() As IE_PageSettings
        Get
            Dim IERegKey As RegistryKey
            IERegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", True)
            Dim ips As New IE_PageSettings(IERegKey.GetValue("header", 0).ToString, IERegKey.GetValue("footer", 0).ToString)
            IERegKey.Close()
            Return ips
        End Get
        Set(ByVal value As IE_PageSettings)
            Dim IERegKey As RegistryKey
            IERegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", True)
            IERegKey.SetValue("header", value.header)
            IERegKey.SetValue("footer", value.footer)
            IERegKey.Flush()
            IERegKey.Close()
        End Set
    End Property

'Using
Dim ips as new IE_PageSettings(""This is page header,"This is page footer") 'or use IE coimmands like &w&bPage &p of &P or leave them blank
Me.IEPageSettings=ips
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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

751 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