Control Output File Name and Location Using Print to File

The TIFF Image Printer, Raster Image Printer and PDF Image Printer have built-in support which allows Print to File to dynamically control the output file location and name when integrating printing to the Image Printers within your own programs and workflow. This technique works for scenarios where you only need to control the output folder and/or the output filename and are always creating the same type of output file.

You can use this method when writing Office automation code for Word, Excel, Publisher, PowerPoint and Visio; Outlook does not have this capability. This approach can be used in VBA macros and scripts, and anywhere else you can use Office automation.

Other applications with a COM interface for printing may support Print to File as well, the print method usually contains arguments to pass a full path as the desired output file name and optionally an argument to enable print to file or for WIN32 applications the DOCINFO structure in the StartDoc API can be used to specify the output file.

When using Print to File, the output name and location is set directly in your code as you print the document, allowing full control over the location and file name at the time of printing. Prompting is automatically suppressed and all other conversion settings for controlling the output file are taken from the Image Printer’s currently selected profile.

Below is a sample WORD VBA macro that that will print the currently open document to a specific folder and create a new generated file name each time. The directory is created if it does not exist as Print to File will fail on a missing directory. The file name is based on the document name and the current date and time.

Sub PrintToFile()
'
' PrintToFile Macro
' Prints the open Word document to the PDF Image Printer with custom location and no prompting
'

    ' Get base document name for output
    Dim documentName As String
    Dim baseDocumentName As String
    documentName = ThisDocument.Name
    If InStrRev(documentName, ".") > 0 Then
        baseDocumentName = Left(documentName, InStrRev(documentName, ".") - 1)
    End If
    
   ' Create the directory if needed
    Dim outputDir As String
    Dim outputDirExists As String
    outputDir = "C:\PEERNETPrintToFile\"
    outputDirExists = Dir(outputDir, vbDirectory)
    If outputDirExists = "" Then
        MkDir (outputDir)
    End If
    
    ' Get currrent date/time for file name
    Dim datetimeStr As String
    datetimeStr = Format(Now(), "MM-DD-YYYY-hh-nn-ss")
    
    ' Create the file name. Print to File requires the full path, including
    ' the file extension of the output file you are creating
    Dim outputFileName As String
    outputFileName = outputDir & baseDocumentName & "-" & datetimeStr & ".tif"
 
    ' Set the printer
    ActivePrinter = "TIFF Image Printer 12"
    
    ' Print the document
    Application.PrintOut PrintToFile:=True, outputFileName:=outputFileName, _
        FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
        wdPrintAllPages, Collate:=True, Background:=False, _
        PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0
End Sub

This is a simple example of using Print to File in a VBA macro. The same approach works in C++, .NET, PowerShell and other scripting languages. In this macro we dynamically generated the output file name from the document and using the current date and time. More advanced workflows where the document, output location and file name are pulled from a database, read from a job file on disk, or passed as arguments from a RESTFUL API or web service are just a few of the ways you can integrate printing to the Image Printers using this method.

Office Print to File Methods

In Word, Excel and Visio, the PrintToFile argument needs to be set to true as well as passing the full path to the desired output file in a separate argument. For Publisher and PowerPoint, supplying the full path in the PrintToFile argument is all that is needed.

WORD
Document.PrintOut(Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)

Excel
Workbook.PrintOutEx(From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, IgnorePrintAreas)

PowerPoint
Presentation.PrintOut(From, To, PrintToFile, Copies, Collate)

Publisher
Document.PrintOutEx(From, To, PrintToFile, Copies, Collate, PrintStyle)

Visio
Document.PrintOut(PrintRange, FromPage, ToPage, ScaleCurrentViewToPaper, PrinterName, PrintToFile, OutputFileName, Copies, Collate, ColorAsBlack)

Included in the Image Printer 12 installation is a C# sample application demonstrating using Print to File from Word, Excel, PowerPoint, Publisher and Visio. Also included is a Word document containing the macro listed above. For more information on Print to File usage and where to find the program samples, see the user guide for your Image Printer.

Print to File TIFF Image Printer
Print to File Raster Image Printer
Print to File PDF Image Printer