Components for Developers & PDF Software → Virtual Printer VB.NET Tutorials

How to create Virtual Printer using VB.NET

 

How to print files programmatically using the ShellExecute function

The code sample below demonstrates how to print files programmatically on either a physical or a virtual printer using the ShellExecute function. It also demonstrates how to change the default system printer.

Declare Function GetDefaultPrinter Lib "winspool.drv" Alias 
"GetDefaultPrinterA" (ByVal szPrinter As StringBuilder,
		ByRef bufferSize As Int32) As Boolean

Declare Function SetDefaultPrinter Lib "winspool.drv" Alias 
"SetDefaultPrinterA" (ByVal szPrinter As String) As Boolean

Public Sub PrintDocumentUsingShellExecute (ByVal szPrinter As String,
		ByVal szDocumentPath As String)

    Dim szDefaultPrinter As StringBuilder = New StringBuilder(256)         
    Dim bufferSize As Integer = szDefaultPrinter.Capacity
     
    ' get the default printer
    GetDefaultPrinter(szDefaultPrinter, bufferSize)

    ' change the default printer
    If String.Compare(szPrinter, szDefaultPrinter.ToString(), True) <> 0 Then
        SetDefaultPrinter(szPrinter)
    End If     

    ' send the document  to the print 
    Dim printProcess As Process = New Process
    printProcess.StartInfo.FileName = szDocumentPath
    printProcess.StartInfo.Verb = "Print"
    printProcess.StartInfo.CreateNoWindow = True
    printProcess.Start()

    ' set default printer back to original
    If String. Compare(szPrinter, szDefaultPrinter.ToString()) <> 0 Then
        SetDefaultPrinter( szDefaultPrinter.ToString())
    End If

End Sub

Then it's necessary to call these functions with the required parameters. For example, you can print MS Word and PDF documents this way:

PrintDocumentUsingShellExecute "Your Virtual Printer", 
						"c:\Documents\AnyDocument.doc"
PrintDocumentUsingShellExecute "Your Virtual Printer", 
						"c:\ Documents \AnyDocument.pdf"

Virtual Printer menu

Latest news

April 27 2010
Now we have a new service: the development of POS-printer.

Send us your comments

We're really eager to hear from you. Please send us your suggestions in one of two ways: