none
printer paper size RRS feed

  • Question

  • hi

     how to get the available paper sizes for the default printer

    code not work

     Private Sub Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button26.Click
            On Error Resume Next
            PaperTest()
        End Sub
       Private Sub PaperTest()
            Dim printerName As String = "Microsoft XPS Document Writer"
            Dim printDoc As New PrintDocument()
            printDoc.PrinterSettings.PrinterName = printerName
            If Not printDoc.PrinterSettings.IsValid Then
                Dim msg As String = String.Format("Can't find printer ""{0}"".", printerName)
                MsgBox(msg)
                Return
            End If
        End Sub


    Monday, December 10, 2018 10:33 AM

Answers

  • I have a combobox that shows all the printers device I want when choosing a printer that shows its paper size

    a4 or pos or barcode label width , height  etic..

    Alright I think I tuned into it finally.

    This example has a combobox for printers and papersizes. When you select a printer the papersizes combobox updates for the selected printer model. The default printer and papersize are also first initialized in the combobox.

    You will need to modify it for your exact needs.

    The performance listing the papersizes is slow. I have noticed that before with this. I am not sure why it is from the ms examples. Maybe you should ask a new question about that.

    The example has two comboboxes and a button as shown. When you select the printer the avalible papersizes combobox is updated. The printdocument is set to the selected printer and papersize ready to print.

    Imports System.Drawing.Printing
    
    Public Class Form6
        Private WithEvents PrintDocument1 As PrintDocument = New PrintDocument
        Private PrintDialog1 As New PrintDialog
        Private PrintPreviewDialog1 As New PrintPreviewDialog
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For i As Integer = 0 To PrinterSettings.InstalledPrinters.Count - 1
    
                ComboBox1.Items.Add(PrinterSettings.InstalledPrinters(i).ToString)
    
                'set to default printer
                If PrintDocument1.PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters(i).ToString Then
                    ComboBox1.SelectedIndex = i
                End If
            Next
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
            Cursor = Cursors.WaitCursor
    
            With PrintDocument1
                'set printdoc to printer selected in combobox1
                .PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters(ComboBox1.SelectedIndex)
    
                If ComboBox2.Items.Count > 0 Then ComboBox2.Items.Clear()
    
                Dim pkSize As PaperSize
                For i As Integer = 0 To .PrinterSettings.PaperSizes.Count - 1
                    pkSize = .PrinterSettings.PaperSizes.Item(i)
                    ComboBox2.Items.Add(pkSize.PaperName)
    
                    'set selected paper combobox2 to default paper for this printer
                    If .PrinterSettings.DefaultPageSettings.PaperSize.PaperName = pkSize.PaperName Then
                        ComboBox2.SelectedIndex = i
                    End If
                Next
    
            End With
    
            Cursor = Cursors.Default
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
            ' Set the print doc paper size based upon the selection in the combo box.
            If ComboBox2.SelectedIndex > -1 And PrintDocument1.DefaultPageSettings.PaperSize IsNot PrintDocument1.PrinterSettings.PaperSizes.Item(ComboBox2.SelectedIndex) Then
    
                PrintDocument1.DefaultPageSettings.PaperSize =
                    PrintDocument1.PrinterSettings.PaperSizes.Item(ComboBox2.SelectedIndex)
            End If
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top,
                    e.MarginBounds.Width, e.MarginBounds.Height)
    
            e.Graphics.DrawString("Page Width: " & e.PageBounds.Width.ToString,
                    New Font("Arial", 18), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
    
        End Sub
    End Class

    • Marked as answer by ahmeddc Tuesday, December 11, 2018 2:44 AM
    Monday, December 10, 2018 11:15 PM

All replies

  • There are samples in the doc PageSettings.PaperSize
    Monday, December 10, 2018 11:28 AM
  • The example shows how to make a list PaperSizes of the available paper sizes for the current print document printer.

    Note it is a list of PaperSize object types.

    Next a listbox is made filled with the string PaperSizes(n).PaperName for each paper object type.

    So those are two different things. First is the PaperSize object type (or kind) which is a list of paper properties including one property which is a string PaperName .

    So its like the PaperSizes is a list of all automobiles and the PaperName is one kind of automobile in the list of all automobiles and one example automobile is named  Porsche.

    See the difference????

    In the Print Button event code you can see one method to set the printer is to use the listbox.selectedindex which sets the PaperSize object type (not used commented out) and the other method is to use a known index from the list of paper types as shown using legal = 1 index in the list of paper types.

    So that is important to realize. The listbox shows KINDS of paper and the actual print button sets the default print paper to index = 1 from the list of paper sizes for the default printer (which is legal = 1 in this printer).

    I think you were confused by that the last time you asked.

    You either need to have the user select the paper or you have to know that the index you set is valid for the printer ie legal = 1.

    The example makes all the controls. Just paste the code into an empty form to run the example.

    Imports System.Drawing.Printing
    
    Public Class Form5
        Private WithEvents printdoc As PrintDocument = New PrintDocument
        Private PaperSizes As New List(Of PaperSize)
        Private PrintPreviewDialog1 As New PrintPreviewDialog
        Private WithEvents PaperSizeList As New ListBox With {.Parent = Me, .Location = New Point(10, 20), .Size = New Size(200, 150)}
        Private WithEvents buttonPrint As New Button With {.Parent = Me, .Location = New Point(100, 200), .Text = "Print"}
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            'make a list of the papersize type
            Dim pkSize As PaperSize
            For i As Integer = 0 To printdoc.PrinterSettings.PaperSizes.Count - 1
                pkSize = printdoc.PrinterSettings.PaperSizes.Item(i)
                PaperSizes.Add(pkSize)
            Next
    
    
            'show the paper names in the list from the paper types list
            For Each p As PaperSize In PaperSizes
                PaperSizeList.Items.Add(p.PaperName)
            Next
    
            'set to index = 1 which is legal
            PaperSizeList.SelectedIndex = 1
    
        End Sub
    
        Private Sub buttonPrint_Click(sender As Object, e As EventArgs) Handles buttonPrint.Click
    
            ' Set the paper size based upon the selection in the combo box.
            If PaperSizeList.SelectedIndex <> -1 Then
                'set to selection 
                'printdoc.DefaultPageSettings.PaperSize = PaperSizes(PaperSizeList.SelectedIndex)
    
                'set to known index legal = 1
                printdoc.DefaultPageSettings.PaperSize = PaperSizes(1)
    
            End If
    
            PrintPreviewDialog1.Document = printdoc
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub printdoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles printdoc.PrintPage
    
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top,
                    e.MarginBounds.Width, e.MarginBounds.Height)
    
            e.Graphics.DrawString("Page Width: " & e.PageBounds.Width.ToString,
                    New Font("Arial", 11), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
    
        End Sub
    End Class


    Monday, December 10, 2018 12:36 PM
  • The example shows how to make a list PaperSizes of the available paper sizes for the current print document printer.

    Note it is a list of PaperSize object types.

    Next a listbox is made filled with the string PaperSizes(n).PaperName for each paper object type.

    So those are two different things. First is the PaperSize object type (or kind) which is a list of paper properties including one property which is a string PaperName .

    So its like the PaperSizes is a list of all automobiles and the PaperName is one kind of automobile in the list of all automobiles and one example automobile is named  Porsche.

    See the difference????

    In the Print Button event code you can see one method to set the printer is to use the listbox.selectedindex which sets the PaperSize object type (not used commented out) and the other method is to use a known index from the list of paper types as shown using legal = 1 index in the list of paper types.

    So that is important to realize. The listbox shows KINDS of paper and the actual print button sets the default print paper to index = 1 from the list of paper sizes for the default printer (which is legal = 1 in this printer).

    I think you were confused by that the last time you asked.

    You either need to have the user select the paper or you have to know that the index you set is valid for the printer ie legal = 1.

    The example makes all the controls. Just paste the code into an empty form to run the example.

    Imports System.Drawing.Printing
    
    Public Class Form5
        Private WithEvents printdoc As PrintDocument = New PrintDocument
        Private PaperSizes As New List(Of PaperSize)
        Private PrintPreviewDialog1 As New PrintPreviewDialog
        Private WithEvents PaperSizeList As New ListBox With {.Parent = Me, .Location = New Point(10, 20), .Size = New Size(200, 150)}
        Private WithEvents buttonPrint As New Button With {.Parent = Me, .Location = New Point(100, 200), .Text = "Print"}
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            'make a list of the papersize type
            Dim pkSize As PaperSize
            For i As Integer = 0 To printdoc.PrinterSettings.PaperSizes.Count - 1
                pkSize = printdoc.PrinterSettings.PaperSizes.Item(i)
                PaperSizes.Add(pkSize)
            Next
    
    
            'show the paper names in the list from the paper types list
            For Each p As PaperSize In PaperSizes
                PaperSizeList.Items.Add(p.PaperName)
            Next
    
            'set to index = 1 which is legal
            PaperSizeList.SelectedIndex = 1
    
        End Sub
    
        Private Sub buttonPrint_Click(sender As Object, e As EventArgs) Handles buttonPrint.Click
    
            ' Set the paper size based upon the selection in the combo box.
            If PaperSizeList.SelectedIndex <> -1 Then
                'set to selection 
                'printdoc.DefaultPageSettings.PaperSize = PaperSizes(PaperSizeList.SelectedIndex)
    
                'set to known index legal = 1
                printdoc.DefaultPageSettings.PaperSize = PaperSizes(1)
    
            End If
    
            PrintPreviewDialog1.Document = printdoc
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub printdoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles printdoc.PrintPage
    
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top,
                    e.MarginBounds.Width, e.MarginBounds.Height)
    
            e.Graphics.DrawString("Page Width: " & e.PageBounds.Width.ToString,
                    New Font("Arial", 11), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
    
        End Sub
    End Class


    I have a combobox that shows all the printers device I want when choosing a printer that shows its paper size

    a4 or pos or barcode label width , height  etic..

    Monday, December 10, 2018 1:03 PM
  • I have a combobox that shows all the printers device I want when choosing a printer that shows its paper size

    a4 or pos or barcode label width , height  etic..

    LOL.

    Maybe show a picture of what you want to do?

    Monday, December 10, 2018 1:54 PM
  • I have a combobox that shows all the printers device I want when choosing a printer that shows its paper size

    a4 or pos or barcode label width , height  etic..

    Alright I think I tuned into it finally.

    This example has a combobox for printers and papersizes. When you select a printer the papersizes combobox updates for the selected printer model. The default printer and papersize are also first initialized in the combobox.

    You will need to modify it for your exact needs.

    The performance listing the papersizes is slow. I have noticed that before with this. I am not sure why it is from the ms examples. Maybe you should ask a new question about that.

    The example has two comboboxes and a button as shown. When you select the printer the avalible papersizes combobox is updated. The printdocument is set to the selected printer and papersize ready to print.

    Imports System.Drawing.Printing
    
    Public Class Form6
        Private WithEvents PrintDocument1 As PrintDocument = New PrintDocument
        Private PrintDialog1 As New PrintDialog
        Private PrintPreviewDialog1 As New PrintPreviewDialog
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For i As Integer = 0 To PrinterSettings.InstalledPrinters.Count - 1
    
                ComboBox1.Items.Add(PrinterSettings.InstalledPrinters(i).ToString)
    
                'set to default printer
                If PrintDocument1.PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters(i).ToString Then
                    ComboBox1.SelectedIndex = i
                End If
            Next
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
            Cursor = Cursors.WaitCursor
    
            With PrintDocument1
                'set printdoc to printer selected in combobox1
                .PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters(ComboBox1.SelectedIndex)
    
                If ComboBox2.Items.Count > 0 Then ComboBox2.Items.Clear()
    
                Dim pkSize As PaperSize
                For i As Integer = 0 To .PrinterSettings.PaperSizes.Count - 1
                    pkSize = .PrinterSettings.PaperSizes.Item(i)
                    ComboBox2.Items.Add(pkSize.PaperName)
    
                    'set selected paper combobox2 to default paper for this printer
                    If .PrinterSettings.DefaultPageSettings.PaperSize.PaperName = pkSize.PaperName Then
                        ComboBox2.SelectedIndex = i
                    End If
                Next
    
            End With
    
            Cursor = Cursors.Default
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
            ' Set the print doc paper size based upon the selection in the combo box.
            If ComboBox2.SelectedIndex > -1 And PrintDocument1.DefaultPageSettings.PaperSize IsNot PrintDocument1.PrinterSettings.PaperSizes.Item(ComboBox2.SelectedIndex) Then
    
                PrintDocument1.DefaultPageSettings.PaperSize =
                    PrintDocument1.PrinterSettings.PaperSizes.Item(ComboBox2.SelectedIndex)
            End If
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top,
                    e.MarginBounds.Width, e.MarginBounds.Height)
    
            e.Graphics.DrawString("Page Width: " & e.PageBounds.Width.ToString,
                    New Font("Arial", 18), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
    
        End Sub
    End Class

    • Marked as answer by ahmeddc Tuesday, December 11, 2018 2:44 AM
    Monday, December 10, 2018 11:15 PM
  • I have a combobox that shows all the printers device I want when choosing a printer that shows its paper size

    a4 or pos or barcode label width , height  etic..

    Alright I think I tuned into it finally.

    This example has a combobox for printers and papersizes. When you select a printer the papersizes combobox updates for the selected printer model. The default printer and papersize are also first initialized in the combobox.

    You will need to modify it for your exact needs.

    The performance listing the papersizes is slow. I have noticed that before with this. I am not sure why it is from the ms examples. Maybe you should ask a new question about that.

    The example has two comboboxes and a button as shown. When you select the printer the avalible papersizes combobox is updated. The printdocument is set to the selected printer and papersize ready to print.

    Imports System.Drawing.Printing
    
    Public Class Form6
        Private WithEvents PrintDocument1 As PrintDocument = New PrintDocument
        Private PrintDialog1 As New PrintDialog
        Private PrintPreviewDialog1 As New PrintPreviewDialog
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For i As Integer = 0 To PrinterSettings.InstalledPrinters.Count - 1
    
                ComboBox1.Items.Add(PrinterSettings.InstalledPrinters(i).ToString)
    
                'set to default printer
                If PrintDocument1.PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters(i).ToString Then
                    ComboBox1.SelectedIndex = i
                End If
            Next
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
            Cursor = Cursors.WaitCursor
    
            With PrintDocument1
                'set printdoc to printer selected in combobox1
                .PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters(ComboBox1.SelectedIndex)
    
                If ComboBox2.Items.Count > 0 Then ComboBox2.Items.Clear()
    
                Dim pkSize As PaperSize
                For i As Integer = 0 To .PrinterSettings.PaperSizes.Count - 1
                    pkSize = .PrinterSettings.PaperSizes.Item(i)
                    ComboBox2.Items.Add(pkSize.PaperName)
    
                    'set selected paper combobox2 to default paper for this printer
                    If .PrinterSettings.DefaultPageSettings.PaperSize.PaperName = pkSize.PaperName Then
                        ComboBox2.SelectedIndex = i
                    End If
                Next
    
            End With
    
            Cursor = Cursors.Default
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
            ' Set the print doc paper size based upon the selection in the combo box.
            If ComboBox2.SelectedIndex > -1 And PrintDocument1.DefaultPageSettings.PaperSize IsNot PrintDocument1.PrinterSettings.PaperSizes.Item(ComboBox2.SelectedIndex) Then
    
                PrintDocument1.DefaultPageSettings.PaperSize =
                    PrintDocument1.PrinterSettings.PaperSizes.Item(ComboBox2.SelectedIndex)
            End If
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top,
                    e.MarginBounds.Width, e.MarginBounds.Height)
    
            e.Graphics.DrawString("Page Width: " & e.PageBounds.Width.ToString,
                    New Font("Arial", 18), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
    
        End Sub
    End Class

    YES

    THANKS

    That's what I want literally

    Tuesday, December 11, 2018 2:44 AM
  • The performance listing the papersizes is slow. I have noticed that before with this. I am not sure why it is from the ms examples. Maybe you should ask a new question about that.

    It is also slow on my OS (Windows 10), 

    but I did a quick test by replacing it with DeviceCapabilities (tested with DC_PAPERNAMES)

    and it is fast

    Tuesday, December 11, 2018 1:00 PM
  • The performance listing the papersizes is slow. I have noticed that before with this. I am not sure why it is from the ms examples. Maybe you should ask a new question about that.

    It is also slow on my OS (Windows 10), 

    but I did a quick test by replacing it with DeviceCapabilities (tested with DC_PAPERNAMES)

    and it is fast


    Oh good!

    Can you show an example since you have it handy there?

    :)

    Tuesday, December 11, 2018 1:09 PM
  • Can you show an example since you have it handy there?

    The test I did with your modified sub :

        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Cursor = Cursors.WaitCursor
            With PrintDocument1
                .PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters(ComboBox1.SelectedIndex)
                If ComboBox2.Items.Count > 0 Then ComboBox2.Items.Clear()
    
                Dim hPrinter As IntPtr
                Dim pDefaults As New PRINTER_DEFAULTS()
                Dim sPrinterName = PrinterSettings.InstalledPrinters(ComboBox1.SelectedIndex)
                Dim sPrinterPort As String = Nothing
                Dim nPrinterPaperSize As Integer = 0
                pDefaults.DesiredAccess = PRINTER_ACCESS_USE
                Dim bResult As Boolean = OpenPrinter(sPrinterName, hPrinter, pDefaults)
                If (bResult = True) Then
                    Dim nBytesNeeded As Integer = -1
                    Dim nSize As Integer = -1
                    Dim pPrinterInfo As IntPtr = IntPtr.Zero
                    GetPrinter(hPrinter, 2, IntPtr.Zero, 0, nBytesNeeded)
                    pPrinterInfo = Marshal.AllocHGlobal(nBytesNeeded)
                    nSize = nBytesNeeded
                    If (Not GetPrinter(hPrinter, 2, pPrinterInfo, nSize, nBytesNeeded)) Then
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error())
                    End If
                    Dim PrinterInfo As PRINTER_INFO_2 = CType(Marshal.PtrToStructure(pPrinterInfo, GetType(PRINTER_INFO_2)), PRINTER_INFO_2)
                    sPrinterPort = PrinterInfo.pPortName
                    Dim pDevMode As IntPtr = PrinterInfo.pDevMode
                    Dim DevMode As DEVMODE = CType(Marshal.PtrToStructure(pDevMode, GetType(DEVMODE)), DEVMODE)
                    nPrinterPaperSize = DevMode.dmPaperSize
                    Marshal.FreeHGlobal(pPrinterInfo)
                    ClosePrinter(hPrinter)
                End If
    
                Dim nNumber As Integer
                nNumber = DeviceCapabilities(sPrinterName, sPrinterPort, DC_PAPERNAMES, IntPtr.Zero, IntPtr.Zero)
                Dim nStringSize As Integer = Marshal.SystemDefaultCharSize * 64
                Dim pNamesBuffer As IntPtr = Marshal.AllocHGlobal(nStringSize * nNumber)
                nNumber = DeviceCapabilities(sPrinterName, sPrinterPort, DC_PAPERNAMES, pNamesBuffer, IntPtr.Zero)
    
                nNumber = DeviceCapabilities(sPrinterName, sPrinterPort, DC_PAPERS, IntPtr.Zero, IntPtr.Zero)
                Dim pPapersBuffer As IntPtr = Marshal.AllocHGlobal(2 * nNumber)
                nNumber = DeviceCapabilities(sPrinterName, sPrinterPort, DC_PAPERS, pPapersBuffer, IntPtr.Zero)
    
                For nCurrent = 0 To nNumber - 1
                    Dim sName As String = Marshal.PtrToStringAuto(CType((CType(pNamesBuffer, Long) + (nStringSize * nCurrent)), IntPtr), 64)
                    Dim nIndex As Integer = sName.IndexOf(vbNullChar)
                    If (nIndex > -1) Then
                        sName = sName.Substring(0, nIndex)
                        ComboBox2.Items.Add(sName)
                        Dim nPaper As Short = Marshal.ReadInt16(((pPapersBuffer + nCurrent * 2)))
                        If (nPaper = nPrinterPaperSize) Then
                            ComboBox2.SelectedIndex = nCurrent
                        End If
                    End If
                Next
                Marshal.FreeHGlobal(pNamesBuffer)
                Marshal.FreeHGlobal(pPapersBuffer)
            End With
            Cursor = Cursors.Default
        End Sub

    With declarations :

     <DllImport("Winspool.drv", SetLastError:=True, EntryPoint:="DeviceCapabilitiesW", CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function DeviceCapabilities(ByVal lpDeviceName As String, ByVal lpPort As String, ByVal iIndex As Integer, ByVal lpOutput As IntPtr, ByVal lpDevMode As IntPtr) As Integer
        End Function
    
        <DllImport("Winspool.drv", SetLastError:=True, EntryPoint:="OpenPrinterW", CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
        Public Shared Function OpenPrinter(ByVal pPrinterName As String, ByRef hPrinter As IntPtr, ByRef pDefault As PRINTER_DEFAULTS) As Boolean
        End Function
    
        <DllImport("Winspool.drv", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
        End Function
    
        <DllImport("Winspool.drv", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function GetPrinter(ByVal hPrinter As IntPtr, ByVal Level As Integer, ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, ByRef pcbNeeded As Integer) As Boolean
        End Function
    
        Public Const ERROR_INSUFFICIENT_BUFFER = 122
        
        
        <StructLayout(LayoutKind.Sequential)>
        Public Structure PRINTER_INFO_2
            <MarshalAs(UnmanagedType.LPTStr)> Public pServerName As String
            <MarshalAs(UnmanagedType.LPTStr)> Public pPrinterName As String
            <MarshalAs(UnmanagedType.LPTStr)> Public pShareName As String
            <MarshalAs(UnmanagedType.LPTStr)> Public pPortName As String
            <MarshalAs(UnmanagedType.LPTStr)> Public pDriverName As String
            <MarshalAs(UnmanagedType.LPTStr)> Public pComment As String
            <MarshalAs(UnmanagedType.LPTStr)> Public pLocation As String
    
            Public pDevMode As IntPtr
    
            <MarshalAs(UnmanagedType.LPTStr)> Public pSepFile As String
            <MarshalAs(UnmanagedType.LPTStr)> Public pPrintProcessor As String
            <MarshalAs(UnmanagedType.LPTStr)> Public pDatatype As String
            <MarshalAs(UnmanagedType.LPTStr)> Public pParameters As String
    
            Public pSecurityDescriptor As IntPtr
            Public Attributes As Integer
            Public Priority As Integer
            Public DefaultPriority As Integer
            Public StartTime As Integer
            Public UntilTime As Integer
            Public Status As Integer
            Public cJobs As Integer
            Public AveragePPM As Integer
        End Structure
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
        Public Structure DEVMODE
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> Public pDeviceName As String
            Public dmSpecVersion As Short
            Public dmDriverVersion As Short
            Public dmSize As Short
            Public dmDriverExtra As Short
            Public dmFields As Integer
            Public dmOrientation As Short
            Public dmPaperSize As Short
            Public dmPaperLength As Short
            Public dmPaperWidth As Short
            Public dmScale As Short
            Public dmCopies As Short
            Public dmDefaultSource As Short
            Public dmPrintQuality As Short
            Public dmColor As Short
            Public dmDuplex As Short
            Public dmYResolution As Short
            Public dmTTOption As Short
            Public dmCollate As Short
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> Public dmFormName As String
            Public dmUnusedPadding As Short
            Public dmBitsPerPel As Integer
            Public dmPelsWidth As Integer
            Public dmPelsHeight As Integer
            Public dmNup As Integer
            Public dmDisplayFrequency As Integer
            Public dmICMMethod As Integer
            Public dmICMIntent As Integer
            Public dmMediaType As Integer
            Public dmDitherType As Integer
            Public dmReserved1 As Integer
            Public dmReserved2 As Integer
            Public dmPanningWidth As Integer
            Public dmPanningHeight As Integer
        End Structure
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
        Public Structure PRINTER_DEFAULTS
            Public pDatatype As IntPtr
            Public pDevMode As IntPtr
            Public DesiredAccess As Integer
        End Structure
        Dim STANDARD_RIGHTS_REQUIRED As Integer = &HF0000
        Dim PRINTER_ACCESS_ADMINISTER As Integer = &H4
        Dim PRINTER_ACCESS_USE As Integer = &H8
        Dim PRINTER_ALL_ACCESS As Integer = (STANDARD_RIGHTS_REQUIRED Or (PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE))
    
        Private Const DC_PAPERS = 2
        Private Const DC_PAPERSIZE = 3
        Private Const DC_PAPERNAMES = 16
        Private Const DC_BINNAMES = 12
        Private Const DC_BINS = 6
        Private Const DEFAULT_VALUES = 0

    • Proposed as answer by tommytwotrain Tuesday, December 11, 2018 4:25 PM
    Tuesday, December 11, 2018 2:42 PM
  • Castor,

    Very good. Much faster with the api.

    Tuesday, December 11, 2018 4:25 PM