Easy way to Print Files to Default Printer

You can use the code given below to print any file that supports the print verb.

The simplest way to find which file-types support the “print” verb is to right click any file and see if “Print” is listed as one of the options in popup menu. Normally all office document types (Word, Excel, PowerPoint), text files etc. support this verb. Verbs are attached to file-types and not to files themselves. So all files of that type will support the verb too.

print verb

The code invokes the “Print” command on the file, and is same as right-clicking any file in Windows Explorer and selecting the “Print” option. All you need to do is call this procedure passing it the full filename with path.

Sub PrintFile(ByVal fileName As String)
    Dim myFile As New ProcessStartInfo
    With myFile
        .UseShellExecute = True
        .WindowStyle = ProcessWindowStyle.Hidden
        .FileName = fileName
        .Verb = "Print"
    End With
    Process.Start(myFile)
End Sub

 Sample Usage:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    PrintFile("C:\abc\Test.txt")
End Sub

 Enjoy! Hot

 

Advertisements

5 Responses to “Easy way to Print Files to Default Printer”

  1. Leandro Alsina Says:

    Like this, but how to invoke the print verb to a group of files instead of a single file?, thanks 🙂

  2. eric Says:

    very nice , thanks.

  3. kristine Says:

    Hi pradeep1210!

    Thank you! what if i want to change the settings of .doc or .pdf file? like its orientation, page/ to print? how does will it be possible?

    • pradeep1210 Says:

      If there is a verb to do that, then you can invoke that verb. Otherwise you are pretty much constrained. You would need to use other ways to do that.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: