1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Printing Text Directly To Default Printer

Discussion in 'Visual Basic [VB]' started by naimish, Jul 8, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    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:
    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
    
     
  2. Saket

    Saket New Member

    Joined:
    Jul 21, 2009
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Don't Know
    Hi, thanks but how can I give option to choose printer to print ?
     
  3. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    :welcome: I will post another article for the same ;)
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
  5. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Printing Text Directly To Selected Printer - Article will be posting on this month :D May Be it will be more useful ;)
     

Share This Page