Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Printing Text Directly To Default Printer (http://www.go4expert.com/articles/printing-text-directly-default-printer-t18417/)

naimish 8Jul2009 11:16

Printing Text Directly To Default Printer
 

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 :p)

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 21Jul2009 11:56

Re: Printing Text Directly To Default Printer
 
Hi, thanks but how can I give option to choose printer to print ?

naimish 21Jul2009 12:23

Re: Printing Text Directly To Default Printer
 
:welcome: I will post another article for the same ;)

shabbir 3Aug2009 14:34

Re: Printing Text Directly To Default Printer
 
Nominate this article for Article of the month - Jul 2009

naimish 20Aug2009 15:54

Re: Printing Text Directly To Default Printer
 
Printing Text Directly To Selected Printer - Article will be posting on this month :D May Be it will be more useful ;)


All times are GMT +5.5. The time now is 11:04.