Solved

printing a html file from a vb.net application

Posted on 2010-08-24
8
317 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
  • 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 69

Expert Comment

by:Éric Moreau
ID: 33511156
0
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 27

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 27

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Open a folder with partial name 14 29
Help with error message for ReportViewer in VS2015 4 26
Expando 4 34
Form design in vb.net 7 20
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…
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…
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…
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…

760 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

21 Experts available now in Live!

Get 1:1 Help Now