none
Print 1 Document To Multiple Printers RRS feed

  • Question

  • Hi,

    I require 1 document to print to multiple printers on our network.

    Call it HR/IT/R&D

    Is there a way to print to these printers? I have found multiple entries to print to local Printers, and to print multiple documents to Default Printer but none to print 1 document to multiple printers. I do have all available IP's and host names of the printers in question. So if there could be an easy way to do this it would be greatly appreciated.

    This is what I Found On MSDN To print to default printer:

    Public Class PrintingExample
        Inherits System.Windows.Forms.Form
        Private components As System.ComponentModel.Container
        Private printButton As System.Windows.Forms.Button
        Private printFont As Font
        Private streamToPrint As StreamReader
        
        Public Sub New()
            ' The Windows Forms Designer requires the following call.
            InitializeComponent()
        End Sub    
        
        ' The Click event is raised when the user clicks the Print button.
        Private Sub printButton_Click(sender As Object, e As EventArgs)
            Try
                streamToPrint = New StreamReader("C:\My Documents\MyFile.txt")
                Try
                    printFont = New Font("Arial", 10)
                    Dim pd As New PrintDocument()
                    AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
                    pd.Print()
                Finally
                    streamToPrint.Close()
                End Try
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub    
        
        ' The PrintPage event is raised for each page to be printed.
        Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)
            Dim linesPerPage As Single = 0
            Dim yPos As Single = 0
            Dim count As Integer = 0
            Dim leftMargin As Single = ev.MarginBounds.Left
            Dim topMargin As Single = ev.MarginBounds.Top
            Dim line As String = Nothing
            
            ' Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
            
            ' Print each line of the file.
            While count < linesPerPage
                line = streamToPrint.ReadLine()
                If line Is Nothing Then
                    Exit While
                End If      
                yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
                ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
                count += 1
            End While
            
            ' If more lines exist, print another page.
            If Not (line Is Nothing) Then
                ev.HasMorePages = True
            Else
                ev.HasMorePages = False
            End If
        End Sub
    
    


    Thanks In Advance

    Jacques

     

    Monday, October 31, 2011 11:11 AM

Answers

  • To List Available Printers:

     

    'cboPrinter is a combobox
            Dim printers As New Printing.PrintDocument()
            Dim printername = printers.PrinterSettings.PrinterName
            For Each printername In printers.PrinterSettings.InstalledPrinters
                cboPrinter.Items.Add(printername)
                cboPrinter.SelectedItem = pdTest.DefaultPageSettings.PrinterSettings.PrinterName
            Next
    

    Just Changes The Printer to Print Document:

     

       Private Sub pdCrRpt_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdCrRpt.PrintPage
    'code for setting up print document
        End Sub
    
    'in another sub:
                Dim ppdPreview As New PrintPreviewDialog()
                ppdPreview.Document = pdCrRpt
    
                ppdPreview.Document.PrinterSettings.PrinterName = strPrinterName
    'where strPrinterName contains the name of the printer you want
    'this is the only change I made to my existing code to make my idea work
    
                ppdPreview.ShowDialog()
    

     

    Sets Default Printer:

     

    Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
        ByVal DriverName As String, ByVal PrinterPort As String)
            Dim DeviceLine As String
    
            'rebuild a valid device line string
            DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort
    
            'Store the new printer information in the
            '[WINDOWS] section of the WIN.INI file for
            'the DEVICE= item
            Call WriteProfileString("windows", "Device", DeviceLine)
    
            'Cause all applications to reload the INI file
            Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
    
        End Sub
        Private Declare Function WriteProfileString Lib "kernel32" Alias     "WriteProfileStringA" _
            (ByVal lpszSection As String, ByVal lpszKeyName As String, _
            ByVal lpszString As String) As Long
        Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
             (ByVal hwnd As Long, ByVal wMsg As Long, _
             ByVal wParam As Long, ByVal lparam As String) As Long
        Private Const HWND_BROADCAST As Long = &HFFFF&
        Private Const WM_WININICHANGE As Long = &H1A
    
    'in a sub:
        SetDefaultPrinter(strPrinterName, "", "")
    'where strPrinterName contains the name of the printer you want
    

    As Seen on: http://vbcity.com/forums/t/68137.aspx

     

     

    • Proposed as answer by JMGous Tuesday, November 1, 2011 4:56 AM
    • Marked as answer by Paul Zhou Tuesday, November 8, 2011 7:59 AM
    Tuesday, November 1, 2011 4:54 AM

All replies

  • Change printers and print again.
    Monday, October 31, 2011 1:45 PM
  • To List Available Printers:

     

    'cboPrinter is a combobox
            Dim printers As New Printing.PrintDocument()
            Dim printername = printers.PrinterSettings.PrinterName
            For Each printername In printers.PrinterSettings.InstalledPrinters
                cboPrinter.Items.Add(printername)
                cboPrinter.SelectedItem = pdTest.DefaultPageSettings.PrinterSettings.PrinterName
            Next
    

    Just Changes The Printer to Print Document:

     

       Private Sub pdCrRpt_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdCrRpt.PrintPage
    'code for setting up print document
        End Sub
    
    'in another sub:
                Dim ppdPreview As New PrintPreviewDialog()
                ppdPreview.Document = pdCrRpt
    
                ppdPreview.Document.PrinterSettings.PrinterName = strPrinterName
    'where strPrinterName contains the name of the printer you want
    'this is the only change I made to my existing code to make my idea work
    
                ppdPreview.ShowDialog()
    

     

    Sets Default Printer:

     

    Private Sub SetDefaultPrinter(ByVal PrinterName As String, _
        ByVal DriverName As String, ByVal PrinterPort As String)
            Dim DeviceLine As String
    
            'rebuild a valid device line string
            DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort
    
            'Store the new printer information in the
            '[WINDOWS] section of the WIN.INI file for
            'the DEVICE= item
            Call WriteProfileString("windows", "Device", DeviceLine)
    
            'Cause all applications to reload the INI file
            Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
    
        End Sub
        Private Declare Function WriteProfileString Lib "kernel32" Alias     "WriteProfileStringA" _
            (ByVal lpszSection As String, ByVal lpszKeyName As String, _
            ByVal lpszString As String) As Long
        Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
             (ByVal hwnd As Long, ByVal wMsg As Long, _
             ByVal wParam As Long, ByVal lparam As String) As Long
        Private Const HWND_BROADCAST As Long = &HFFFF&
        Private Const WM_WININICHANGE As Long = &H1A
    
    'in a sub:
        SetDefaultPrinter(strPrinterName, "", "")
    'where strPrinterName contains the name of the printer you want
    

    As Seen on: http://vbcity.com/forums/t/68137.aspx

     

     

    • Proposed as answer by JMGous Tuesday, November 1, 2011 4:56 AM
    • Marked as answer by Paul Zhou Tuesday, November 8, 2011 7:59 AM
    Tuesday, November 1, 2011 4:54 AM