title: Trouble printing to a specific Tray (PaperSource) using VB.NET PrinterSettings
body: <p>I've been having trouble automatically selecting a Tray (PaperSource) to print from on a specific printer (HP OfficeJet Pro 9020 series) using VB.NET. I have reduced my code to a bare minimum below to indicate the process I'm following.</p>
  <p>After the print dialog has popped up and you select 'Tray 2' from the list. The line of code <code>prn.PrinterSettings = pd.PrinterSettings</code> applies that setting, and it subsequently prints from the correct tray. If, however, you just click ok on the dialog it prints from the wrong tray.</p>
  <p>The strange thing is that, as you can see, I set the 'Tray 2' option in code, so, in theory that object should be exactly the same. I used <code>Newtonsoft.JSON</code> to serialise the <code>pd.PrinterSettings</code> object before and after I had selected 'Tray 2' in the dialog so I could compare the differences and they were both identical. The only differences at first were the ones I addressed in the code earlier on. They didn't make any difference though. Those being;</p>
  <pre><code>pd.PrinterSettings.PrintFileName = &quot;IP_192.168.1.50&quot;
  pd.PrinterSettings.PrinterName = &quot;Dispatch Office&quot;
  pd.PrinterSettings.Collate = False
  </code></pre>
  <p>But for whatever reason, it only works if I explicitly open the dialog and select the desired Tray and click ok. <code>prn.PrinterSettings = pd.PrinterSettings</code> is the only line of code that has any effect on what printer is used, so somewhere in <code>pd.PrinterSettings</code> there must be something missing that I couldn't see in the JSON string. I've had success with this code on other printers, but this specific printer seems to be different, but the problem has to be with VB.NET I would think.</p>
  <p>Has anyone come across anything like this before? Here is my full code below.</p>
  <pre><code>Private Sub Print()
     Dim pd As New PrintDialog
  
     pd.PrinterSettings.PrintFileName = &quot;IP_192.168.1.50&quot;
     pd.PrinterSettings.PrinterName = &quot;Dispatch Office&quot;
     pd.PrinterSettings.Collate = False
  
     For Each paperSource As Printing.PaperSource In pd.PrinterSettings.PaperSources
        If paperSource.SourceName = &quot;Tray 2&quot; Then
           pd.PrinterSettings.DefaultPageSettings.PaperSource = paperSource
           Exit For
        End If
     Next
  
     If pd.ShowDialog = DialogResult.OK Then
        Dim prn As New Printing.PrintDocument
  
        prn.PrinterSettings = pd.PrinterSettings
  
        AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
        prn.Print()
        RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
     End If
  End Sub
  
  Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
     Dim myFont As New Font(&quot;Courier New&quot;, 9)
     Dim strTextFile = IO.File.ReadAllText(&quot;C:\textfile.txt&quot;)
     args.Graphics.DrawString(strTextFile, New Font(myFont, FontStyle.Regular), Brushes.Black, 50, 50)
  End Sub
  </code></pre>
  <p>Also, just to confirm, this is independent of the print dialog.
  The code below which is in the shortest form possible also does not work.</p>
  <pre><code>Private Sub PrintAuto()
     Dim ps As New Printing.PrinterSettings With {.PrinterName = &quot;Dispatch Office&quot;}
     ps.DefaultPageSettings.PaperSource = ps.PaperSources(3) ' Tray 2
     Dim prn As New Printing.PrintDocument With {.PrinterSettings = ps}
  
     AddHandler prn.PrintPage, AddressOf PrintPageHandler
     prn.Print()
     RemoveHandler prn.PrintPage, AddressOf PrintPageHandler
  End Sub
  </code></pre>