URGENT: Print to file automatically
Hey guys! I want to print my DataReport directly to file, PRINTTOFILE, irrespective of the system's printer's settings.
Upto now I had tried doing this: I set the printer's settings to FILE: port (manually) and then supply the filename to the "Print To File" dialog box (automatically through API), and then after I change the printer's port settings to the normal one (manually).
What I need is, the software should directly print to the FILE, like the Microsoft Word does, while printing to FILE it will prompt for the FILE name by itself, I want to do it programmatically.
Can anyone help me how to do this!!!!!!!!!!! Tons of Thanks in advance.
[694 byte] By [
daftaryyog] at [2007-11-20 10:58:16]

# 1 Re: URGENT: Print to file automatically
Look at this:
Option Explicit
Private Sub Command1_Click()
Dim printDlg As PrinterDlg
Set printDlg = New PrinterDlg
' Set the starting information for the dialog box based on the current
' printer settings.
printDlg.PrinterName = Printer.DeviceName
printDlg.DriverName = Printer.DriverName
printDlg.Port = Printer.Port
' Set the default PaperBin so that a valid value is returned even
' in the Cancel case.
printDlg.PaperBin = Printer.PaperBin
' Set the flags for the PrinterDlg object using the same flags as in the
' common dialog control. The structure starts with VBPrinterConstants.
printDlg.Flags = VBPrinterConstants.cdlPDNoSelection _
Or VBPrinterConstants.cdlPDNoPageNums _
Or VBPrinterConstants.cdlPDReturnDC
Printer.TrackDefault = False
' When CancelError is set to True the ShowPrinterDlg will return error
' 32755. You can handle the error to know when the Cancel button was
' clicked. Enable this by uncommenting the lines prefixed with "'**".
'**printDlg.CancelError = True
' Add error handling for Cancel.
'**On Error GoTo Cancel
If Not printDlg.ShowPrinter(Me.hWnd) Then
Debug.Print "Cancel Selected"
Exit Sub
End If
'Turn off Error Handling for Cancel.
'**On Error GoTo 0
Dim NewPrinterName As String
Dim objPrinter As Printer
Dim strsetting As String
' Locate the printer that the user selected in the Printers collection.
NewPrinterName = UCase$(printDlg.PrinterName)
If Printer.DeviceName <> NewPrinterName Then
For Each objPrinter In Printers
If UCase$(objPrinter.DeviceName) = NewPrinterName Then
Set Printer = objPrinter
End If
Next
End If
' Copy user input from the dialog box to the properties of the selected printer.
Printer.Copies = printDlg.Copies
Printer.Orientation = printDlg.Orientation
Printer.ColorMode = printDlg.ColorMode
Printer.Duplex = printDlg.Duplex
Printer.PaperBin = printDlg.PaperBin
Printer.PaperSize = printDlg.PaperSize
Printer.PrintQuality = printDlg.PrintQuality
' Display the results in the immediate (Debug) window.
' NOTE: Supported values for PaperBin and Size are printer specific. Some
' common defaults are defined in the Win32 SDK in MSDN and in Visual Basic.
' Print quality is the number of dots per inch.
With Printer
Debug.Print .DeviceName
If .Orientation = 1 Then
strsetting = "Portrait. "
Else
strsetting = "Landscape. "
End If
Debug.Print "Copies = " & .Copies, "Orientation = " & _
strsetting
If .ColorMode = 1 Then
strsetting = "Black and White. "
Else
strsetting = "Color. "
End If
Debug.Print "ColorMode = " & strsetting
If .Duplex = 1 Then
strsetting = "None. "
ElseIf .Duplex = 2 Then
strsetting = "Horizontal/Long Edge. "
ElseIf .Duplex = 3 Then
strsetting = "Vertical/Short Edge. "
Else
strsetting = "Unknown. "
End If
Debug.Print "Duplex = " & strsetting
Debug.Print "PaperBin = " & .PaperBin
Debug.Print "PaperSize = " & .PaperSize
Debug.Print "PrintQuality = " & .PrintQuality
If (printDlg.Flags And VBPrinterConstants.cdlPDPrintToFile) = _
VBPrinterConstants.cdlPDPrintToFile Then
Debug.Print "Print to File Selected"
Else
Debug.Print "Print to File Not Selected"
End If
Debug.Print "hDC = " & printDlg.hDC
End With
rtb.SelPrint printDlg.hDC
' PrintForm ' prints form to selected printer
Exit Sub
'**Cancel:
'**If Err.Number = 32755 Then
'** Debug.Print "Cancel Selected"
'**Else
'** Debug.Print "A nonCancel Error Occured - "; Err.Number
'**End If
End Sub
http://support.microsoft.com/kb/322710/EN-US/#7