Please enable JavaScript to view this site.

Navigation: Developer Information > Programming Environments > Visual Basic 6.0

Using the Printer object

Scroll Prev Top Next More

The Visual Basic printer object greatly simplifies sending output to a printer. The following is a very simple example of creating a PDF and specifying the output file name.

 

Private Sub Command1_Click()

 'set the output file name

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFFileName", "c:\TEMP\TEST.PDF"

 

 'output some text

 Printer.Print "hello world!"

 

 'save the PDF file

 Printer.EndDoc

End Sub

 

Win2PDF supports all of the standard Printer object methods and properties. A more advanced example requires some support routines for writing a Dword value to the registry. The following code must be added to the declarations section of the sample form.

 

Option Explicit

 

Private Const REG_DWORD As Long = 4

Private Const HKEY_CURRENT_USER = &H80000001

Private Const KEY_ALL_ACCESS = &H3F

 

Private Declare Function RegCloseKey Lib "advapi32.dll" _

           (ByVal hKey As Long) As Long

Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _

  "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _

  ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _

  Long) As Long

Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _

  "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _

  ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _

  ByVal cbData As Long) As Long

 

Private Sub SaveWin2PDFDword(sValueName As String, lValueSetting As Long)

 Dim lRetVal As Long     'result of the SetValueExLong function

 Dim hKey As Long      'handle of open key

 

 lRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\VB and VBA Program Settings\Dane Prairie Systems\Win2PDF", 0, _

             KEY_ALL_ACCESS, hKey)

 lRetVal = RegSetValueExLong(hKey, sValueName, 0&, _

             REG_DWORD, lValueSetting, 4)

 RegCloseKey (hKey)

End Sub

 

Now we can use the subroutine to save a Dword value to the registry and enable 128 bit encryption. The encryption options are only valid for Win2PDF Pro.

 

Private Sub Command1_Click()

 'set the output file name

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFFileName", "c:\TEMP\TEST.PDF"

 

 'enable 128 bit encryption (Win2PDF Pro only) and automatic url detection

 SaveWin2PDFDword "file options", &H30

 

 'set a master password

 SaveSetting "Dane Prairie Systems", "Win2PDF", "master password", "abracadabra"

 

 'set the PDF document title

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFTitle", "Hello World sample"

 

 'set landscape orientation

 Printer.Orientation = vbPRORLandscape

 

 'output some text

 Printer.Print "hello world!"

 Printer.Print "www.win2pdf.com"

 

 'save the PDF file

 Printer.EndDoc

End Sub

 

The following example demonstrates how to append to an existing PDF file.

 

Private Sub Command2_Click()

 

 'set the output file name to create original PDF file

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFFileName", "c:\TEMP\TEST.PDF"

 

 'output some text

 Printer.Print "hello world!"

 

 'save the PDF file

 Printer.EndDoc

 

 

 'Append new text to original PDF file

 'assign original PDF file

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFAppendFile", "c:\TEMP\TEST.PDF"

 

 'set new output file to maintain the original PDF file

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFFileName", "c:\AppendedTest.PDF"

 

 Printer.Print "2nd page added"

 Printer.EndDoc

 

End Sub

 

The following example enables the "Send PDF" option, sets the email subject, sets the email address, sets the email body text, and then creates the PDF file:

 

 

 'enable sending the PDF file

 'replace the file options with &H421 to email with no user interaction using the Mail Helper application

 SaveWin2PDFDword "file options", &H21

 

 'set the email subject (optional)

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFMailSubject", "Test Subject"

 

 'set the recipient email address (optional)

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFMailRecipients", billg@microsoft.com

 

 'set the email body text (optional)

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFMailNote", "The requested PDF file is attached."

 

 'set the PDF file name

 SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFFileName", "c:\\test.pdf"

 

 'output some text

 Printer.Print "hello world!"

 Printer.Print www.win2pdf.com

 

 'save the PDF file

 Printer.EndDoc