Technology Software

How to Print a File Using Visual Basic 6.0

    • 1). Create a new Standard EXE project in Visual Basic by clicking "File,"then"New Project" and choosing the Standard EXE template. Add a Label control to the form that opens by clicking on the Label icon in the Toolbox showing on the left. This is the large letter "A" in caps. Look in the Properties list on the right for the Label1 properties and change the Caption to read "Name of File to Be Printed?" but do not include the quotation marks.

    • 2). Add a TextBox control to the form in the same manner, using the Toolbox icon of a small square with lower case letters "ab" in it. Position the Textbox to the right of the Label. Locate the "Text" property in the properties list for this control and double click on the Text property to highlight the default name. Press the "Delete"on your keyboard to remove it.

    • 3). Add two command buttons from the ToolBox, identified by a small rectangle. Change the Caption of the first to "Print" and the Caption of the second to "Cancel". Add a control to the Toolbox by clicking on "Project" in the top menu and then "Components. Find the "Microsoft Rich Textbox Control 6.0 (SP6)" in the list and click on the check box to select it. Click "Apply" and "OK." Add the new control, which should be at the bottom of the control list. Moving the cursor over the icon will display the name.

    • 4). Click on the Microsoft Rich Textbox Control 6.0 (SP6) on the form. In the Properties list for this control, delete the "Text" name, change the "Multiline" property from "False" to "True, make the "Visible" property "False" and set the "Font" property to whatever font and size you wish.

    • 5). Double click on the Print control to open the Code window. Enter this code exactly as it appears below:

      Private Sub Command1_Click()
      Dim strFileToPrint, strFullFile, strReadLine As String
      strFileToPrint = Text1.Text
      Open strFileToPrint For Input As #1
      Do Until EOF(1)
      Line Input #1, strReadLine
      strFullFile = strFullFile & strReadLine
      Loop
      RichTextBox1 = strFullFile
      RichTextBox1.Visible = True
      RichTextBox1.SelLength = 0
      RichTextBox1.SelPrint Printer.hDC
      End Sub

      Below this type the following code:

      Private Sub Command2_Click()
      End
      End Sub

    • 6). Press "F5" to run the program and print a file. If the document does not print, check the lines of code above to be sure you entered them correctly. If a warning appears to the effect the file was not found, then double check the location and file name.



Leave a reply