title: "PrintTicket provider failed to convert DEVMODE to PrintTicket"
body: <p>few years ago I wrote a WPF (Visual Basic 2012 based) app which included "printing" some report using standard WPF printdialogs/paginators/features....
  everyting worked fine since then (2011/2015), and I did not change a line of that working code...
  Now I had to change something (not printing related) to that project and of course I checked the printing features as well...
  now the "printing" code fails when using it... actually is not the "paginator" code, but accessing the PrintTicket/Que objects fails reporting
  "PrintTicket provider failed to convert PrintTicket to DEVMODE. Win32 error: The parameter is incorrect.  "</p>
  
  <p>but it's weird as it does not always fail, and it fails differently with different printers...
  using for instance "PDF Creator" virtual printer, it fails writing/settings some properties of my Que object and not others, while using a different printer fails on others...
  for instance, PDF Creator virtual printer:</p>
  
  <p>1) myQue.DefaultPrintTicket = myTicket = OK
  2) myQue.CurrentJobSettings.Description = "some title" = OK with some printer, FAILS with a different printer</p>
  
  <p>but 2) only fails the very first time it is set, and I can "resolve" "looping" a few times with a minor time sleep.... again, the second or third time it succeded... 
  so I wrote an ugly code like</p>
  
  <p>Dim dlg As New PrintDialog
  .....</p>
  
  <p>myTicket = dlg.PrintTicket<br>
  myQue = dlg.PrintQueue</p>
  
  <p>'-----</p>
  
  <p>' to check in a loop if the interesting </p>
  
  <p>' properties has been set</p>
  
  <p>Dim ticketIsSet(1) As Boolean</p>
  
  <p>For iLoop As Integer = 1 To 5</p>
  
  <pre><code>'it can fail with DEVMODE = NULL
  Try
      If Not ticketIsSet(0) Then
          myQue.DefaultPrintTicket = myTicket
          ticketIsSet(0) = True
      End If
  
      If Not ticketIsSet(1) Then
          myQue.CurrentJobSettings.Description = Me.Title
          ticketIsSet(1) = True
      End If
  Catch ex As Exception
      Errors.Add(ex.Message)
      System.Threading.Thread.CurrentThread.Sleep(1000)
  End Try
  If ticketIsSet(0) AndAlso ticketIsSet(1) Then Exit For
  </code></pre>
  
  <p>Next</p>
  
  <p>If Errors.Count Then</p>
  
  <pre><code>' it's still ok if title can't be set... :( 
  If ticketIsSet(0) Then Errors.Clear()
  </code></pre>
  
  <p>End If</p>
  
  <p>so far, "so good" as I get the job done... but only partially... as my Paginator is used to (succesfully) populate a System.Windows.Controls.DocumentViewer... this alternate documentviewer window can later actually print if requested...
  and again, with different printers it succed or fail with "PrintTicket provider failed to convert PrintTicket to DEVMODE. Win32 error: The parameter is incorrect.  " exception...</p>
  
  <p>with PDF Creator virtual printer it succed, with a different physical printer it fails...</p>
  
  <p>I tried the very same looping trick as above, like</p>
  
  <p>Dim Errors As New Specialized.StringCollection</p>
  
  <p>Dim bDone As Boolean = False</p>
  
  <p>For iLoop As Integer = 1 To 5</p>
  
  <pre><code>Try
  
      Dim writer As System.Windows.Xps.XpsDocumentWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(m_printQueue)
      writer.Write(Me.docViewer.Document.DocumentPaginator, Me.m_printTicket)
      writer = Nothing
      bDone = True
  
  Catch ex As Exception
      Errors.Add(ex.Message)
      System.Threading.Thread.CurrentThread.Sleep(1000)
  End Try
  
  If bDone Then Exit For
  </code></pre>
  
  <p>Next
  If bDone Then Errors.Clear()</p>
  
  <p>If Errors.Count &lt;> 0 Then BasShowErrors(Errors, Me, False)</p>
  
  <p>and here it fails (again, on some printers) on 
  writer.Write(Me.docViewer.Document.DocumentPaginator, Me.m_printTicket)
  with "PrintTicket provider failed to convert PrintTicket to DEVMODE. Win32 error: The parameter is incorrect.  " exception...</p>
  
  <p>obviously that printer is ok as regards other programs like World, Excel and the like, and it's ok as well with all other (NOT WPF based) projects... and again this only occur "NOW", as it worked like a charme at the time of the initial development...</p>
  
  <p>my current settings are:</p>
  
  <p>Win7 Ultimate sp 1</p>
  
  <p>SQL 2014 Dev Edition (not relevant)</p>
  
  <p>Visual Studio 2012 Ultimate Version 11.0.61219.00 Update 5</p>
  
  <p>Microsoft .NET Framework version 4.6.01055</p>
  
  <p>I even tried "repairing" the NET Framework with NetFramwork Repair Tool (<a href="https://www.microsoft.com/en-us/download/details.aspx?id=30135" rel="nofollow noreferrer">https://www.microsoft.com/en-us/download/details.aspx?id=30135</a>) with no success...</p>
  
  <p>any hint is very appreciated :)</p>
  
  <h2>TIA</h2>
  
  <p>asql</p>