Printing Text Directly To Default Printer

naimish's Avatar author of Printing Text Directly To Default Printer
This is an article on Printing Text Directly To Default Printer in Visual Basic [VB].
Rated 5.00 By 2 users

Introduction



This code once used, it will directly print the data of Text Box to any default selected printers.

Adv.

- If Printer are on any other server and properly selected, this code will work properly fine.

Dis. Adv.

- N/A

Background



To Print Text Directly To Default Printer.

Please do the follow steps.
  1. Open Visual Basic 6.0 (New Project)
  2. Add below references to it.
    • Microsoft Word 11.0 Object Library
    • Microsoft Scripting Run Time
  3. Add below Componenet to it.
    • Microsoft Common Dialog Control 6.0 (SP6)
  4. On Form1, Insert Two Textboxes and One Microsoft Common Dialog Control 6.0 (SP6)
  5. Open Code view, copy & paste below code into it. (At your own risk )

The code



Code: VB
Option Explicit
Dim FilePath As String
'***************************************
' Module/Form Name : PrntForm
'
' Purpose : To Print The Data
'
' Author : NAIMISH
'
' Date Author Description
' ------------------------------------------------------------------------------------------------
' 08/07/2009 NAIMISH PrntForm will take the data from the screen's textbox and
' print them by using defualt selected printer into the system.
'*************************************************************************************************
Public Sub PrntForm()
    Dim objWord As Word.Application
    Set objWord = New Word.Application
    Dim oFile As New Scripting.FileSystemObject
    Dim FileExists As Boolean
    ' Global Variable
    FilePath = ""
    ' Check for File existance
    FileExists = oFile.FileExists("C:\Temp.doc")
    If Not FileExists = False Then
        objWord.Documents.Add "C:\Temp.doc", , , True
    Else
        MsgBox "Layout file not found.", vbOKOnly, "Temp.doc File Not Found"
        Exit Sub
    End If
   
    objWord.ActiveDocument.Bookmarks("FirstName").Select
    objWord.Selection.TypeText Text1.Text
    objWord.ActiveDocument.Bookmarks("LastName").Select
    objWord.Selection.TypeText Text2.Text
    objWord.ActiveDocument.PrintOut
    Do While objWord.BackgroundPrintingStatus > 0
    Loop
    Call SaveMyFile 'Get the path to save .doc file
    'Save it
    If Not FilePath = "" Then
        objWord.ActiveDocument.SaveAs FilePath
    End If
    objWord.ActiveDocument.Close wdDoNotSaveChanges
    objWord.Quit wdDoNotSaveChanges
    Set objWord = Nothing
End Sub

Public Sub SaveMyFile()
    On Error GoTo errhandler
    
    CommonDialog1.CancelError = True
    CommonDialog1.Filter = "Word Document (*.doc)|*.doc"
    CommonDialog1.FileName = ""
    CommonDialog1.ShowSave
    
    FilePath = CommonDialog1.FileName
    
    Exit Sub
    errhandler:
    Select Case Err
        Case 32755 ' Dialog Cancelled
            MsgBox "You have cancelled to save the file.", vbOKOnly, "VB 6.0"
        Case Else
            MsgBox "Unexpected error. Err " & Err & " : " & Error
    End Select
End Sub

Private Sub Form_Load()
    Call PrntForm
End Sub
Saket's Avatar
Banned
Hi, thanks but how can I give option to choose printer to print ?
naimish's Avatar
Banned
I will post another article for the same
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nominate this article for Article of the month - Jul 2009
naimish's Avatar
Banned
Printing Text Directly To Selected Printer - Article will be posting on this month May Be it will be more useful