How to print without print dialog?

Have a personal application that prints to a DYMO printer that always print to the same form and printer. It is possible to code it so that just telli it to ‘just’ print without having dialogs. If so how?
I am currently using the following code:

  Dim charWidth, xOne, yOne, yPos, yPosMsg As Double
  Dim num As Integer
  Dim txtOut As String
  Dim g As graphics
  
  ps = New printerSetup
  
  ReadPrinterString(ps)
  
  // set things to get printers maximum resolution
  ps.MaxHorizontalResolution = -1
  ps.MaxVerticalResolution = -1
  
  if ps <> nil then    // just incase
    g = OpenPrinterDialog(ps)
    
    If g <> Nil Then  // just in case
      // Set scaling factors based on PrinterSetup resolutions
      xScaleFactor = ps.HorizontalResolution / 72.0
      yScaleFactor = ps.VerticalResolution / 72.0
      xOne = 1.0 * xScaleFactor  // scale for 1 horizontally
      yOne = 1.0 * yScaleFactor  // scale for 1 vertically
      
      //lets print
      For num = 1 To 1  // loop for the pages, also supplies number to print
        
        // draw position strings at top and bottom of page
        g.PenHeight = 1 * yScaleFactor
        g.PenWidth = 1 * xScaleFactor
        g.TextSize = 12.0 * yScaleFactor  // draw position strings at 12 point text
        g.ForeColor = RGB(0, 0, 0)  // draw following in black
        ypos = 1 * yOne + g.TextAscent  // 5 pts down from top border line (+3 for border width)
        yPosMsg = yPos + 0 * g.TextHeight  // vert pos for scaling factor line
        
        txtOut = ContNum
        g.DrawString(txtOut, 0 * xOne, ypos)
        
        // set new ypos for 5 points up from bottom border. again, RB does
        // not give all of the font metrics to get this really nicely positioned
        yPos = g.Height - 8 * yOne - g.TextHeight + g.TextAscent
        txtOut = PrevCon + "-" + NextCon
        g.DrawString(txtOut, 0 * xone, yPos+80)
        
        g.TextSize = 9.0 * yScaleFactor
        
        if len(MedName) > 12 then
          txtOut = left(MedName,12)
          txtOut = txtOut +"-"
          yPosMsg = yPosMsg + g.TextHeight + 8
          g.DrawString(txtOut, 0 , yPosMsg)
          MedName = mid(MedName,12)
        end if
        
        if len(MedName) > 12 then
          txtOut = left(MedName,12)
          txtOut = txtOut +"-"
          yPosMsg = yPosMsg + g.TextHeight + 8
          g.DrawString(txtOut, 0 , yPosMsg)
          MedName = mid(MedName,12)
        end if
        
        yPosMsg = yPosMsg + g.TextHeight + 8
        txtOut = MedName
        g.DrawString(txtOut, 0 , yPosMsg)
        
        //send this page to the printer, move to the next last page is sent on printer close
        If num < 1 Then g.NextPage
        
      Next Num
    Else
      'MsgBox("Unable to print: graphics object passed to printExample method is Nil.")
    End If
  end if
  

[code]
Const kPrinterStringFile = “DYMO_LabelWriter_450_Turbo_2 RXinfo”

'Method ReadPrinterString
'with a p parameter by byref as printersetup

Dim f, fs As FolderItem
Dim bs As BinaryStream
Dim mb As MemoryBlock

fs = SpecialFolder.Documents.Child(kPrinterStringFile)
f = New FolderItem(fs)

If f <> Nil And f.Exists Then
// retrieve the setup string from the file if it exists
bs = BinaryStream.Open(f, False)
mb = DecodeBase64(bs.Read(bs.Length))
Try
p.SetupString = mb
Catch e As IOException
MsgBox(“Error loading prnsetup”)
return
End Try
Dim i as integer = instr(p.SetupString,“DYMO_LabelWriter_450_Turbo_2”)
if i > 0 then return
end if
If p.PageSetupDialog Then
// give the user a chance to create a new setup string
mb = p.SetupString
bs = BinaryStream.Create(f, True)
bs.Write(EncodeBase64(mb))
End If

Return[/code]

The ReadPrintString routing save the Setupstring, eliminating the need to reenter setup info again.

However the info from the OpenPrinterDialog is not saved so that dialog has to be taken of again. Is it posible to save it and reuse it and use the save string?

g = OpenPrinter(ps) ?

[quote=292679:@Emile Schwarz]

g = OpenPrinter(ps) ?[/quote]
Does not print.

Try changing the subject to something more lucrative like, “How to print without print dialog?”

It may not be possible on OS X because of system security and it being an OS built around protecting the user, but who knows maybe there’s something that will work :slight_smile:

ok

What does it do?
Maybe settings are incomplete?

[quote=292711:@Christian Schmitz]What does it do?
Maybe settings are incomplete?[/quote]
When I changed:
g=OpenPrinterDIalog(PS) to g=OenPrinter(ps) ; nothing print whereaa the first one prints, after responding to the dialog.

A thought:

AFTER you read the printer string from file, you amend it

[code]ReadPrinterString(ps)

// set things to get printers maximum resolution
ps.MaxHorizontalResolution = -1
ps.MaxVerticalResolution = -1[/code]

Does it work if you omit the printerresolution lines (as they should be part of the saved string anyway)

By the way, what does your SAVEPrinterSetupString look like? Is it saving correctly?

I stopped using saved strings some years back.
You might try this:

ps = New printerSetup ps.MaxHorizontalResolution = -1 ps.MaxVerticalResolution = -1 if instr(p.SetupString,"DYMO_LabelWriter_450_Turbo_2") > 1 then // we have the setup for the Dymo g = OpenPrinter(ps) else g = OpenPrinterDialog(ps) end if