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.
- Open Visual Basic 6.0 (New Project)
- Add below references to it.
- Microsoft Word 11.0 Object Library
- Microsoft Scripting Run Time
- Add below Componenet to it.
- Microsoft Common Dialog Control 6.0 (SP6)
- On Form1, Insert Two Textboxes and One Microsoft Common Dialog Control 6.0 (SP6)
- 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
|