Solved

printing a html file from a vb.net application

Posted on 2010-08-24
8
320 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 70

Expert Comment

by:Éric Moreau
ID: 33511156
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with error when uploading excel file 3 38
Adding Existing Item to vb.net Project causes Errors 4 44
get combo value in class 5 14
Name Space error VS2015 1 24
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
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…

860 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