Components for Developers & PDF Software → Virtual Printer VB6 Tutorials

How to create Virtual Printer using VB6

 

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.

Private Declare Function GetDefaultPrinter Lib "winspool.drv" Alias 
"GetDefaultPrinterA" (ByVal szPrinter As String, bufferSize As Long) As Long

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

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

    Dim szDefaultPrinter, szNamePrinterBuff As String
    Dim bufferSize As Long

    ' get the default printer
    GetDefaultPrinter vbNullChar, bufferSize
    szNamePrinterBuff = Space$(bufferSize)
    GetDefaultPrinter szNamePrinterBuff, bufferSize

    szDefaultPrinter = Left$(szNamePrinterBuff, InStr(szNamePrinterBuff, 
		vbNullChar) - 1)

    ' change the default printer
    If szPrinter <> szDefaultPrinter Then
        SetDefaultPrinter szPrinter
    End If

    ' send the document to the print
    ShellExecute 0, "print", szDocumentPath,  vbNullString, vbNullString, SW_HIDE

    ' set the default printer back to original
    If szPrinter <> szDefaultPrinter Then
        SetDefaultPrinter szDefaultPrinter
    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

December 12 2011
Our driver now supports Windows 8

Send us your comments

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