0

I have an app which monitors a network location for new documents. They can be word documents, PDF files, spreadsheets etc.

When a document is detected, it is copied to a local folder within c:\Temp.

What I need is for the document, once copied, to be sent to a specified (not default) printer.

Has anyone got any ideas on how I can achieve this?

Thanks!!

1 Answer 1

2

You may need to create a variety of functions to print your document. Like using LPR to print PDF or PostScript, Microsoft.Office.Interop for Word and Excel documents, and so on...
If it were me, I would use System.IO.Path.GetExtension(filename) to determine the file type and then call whichever function was most appropriate...or log that the file was not printable if the format was not handled.


Microsoft.Office.Interop
Using Microsoft.Office.Interop.Excel, you can call the PrintOutEx method on a Workbook or Worksheet and specify which printer to use.
See:
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.worksheets.printoutex.aspx
and
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook.printoutex.aspx

With Microsoft.Office.Interop.Word you can set the ActivePrinter property of the Application and then use the PrintOut method on the Document.
See:
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._application.activeprinter.aspx
and
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.printout.aspx


LPR
I wrote a tool once that prints PDF and PostScript files. The code was something like:

    'Set the IP address of the printer to use.
    If printer1 Then
        printserver = printer1Address
    ElseIf printer2 Then
        printserver = printer2Address
    ElseIf printer3 Then
        printserver = printer3Address
    End If

    'Use LPR to print the file.
    Dim lprProcess As New Process()
    With lprProcess.StartInfo
        .UseShellExecute = False
        .FileName = "CMD"
        .Arguments = "/c LPR -s " & printserver & " -P P3 " & myNewFileName
    End With
    lprProcess.Start()

LPR is not a included by default in Windows 7 and above, but it is a cake-walk to turn on. See steps 1-3 on http://campus.mst.edu/cis/desktop/documentation/pc/win7_x64/lpr_printer/install.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.