[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

printing in VB

I am the newbie in VB how can I print a text file in printer and some text i want to bold it before print out ?????
  • 2
1 Solution
Several possibilities to print a textfile:
Open "c:\myfile.txt" For Input As #1
Do while Not Eof(1)
   Line Input #1, x$
   Printer.Print x$
Close 1

2) Add a common dialog control named: cdl

Dim iFileNum As Integer, s As String
  If Len(cdl.filename) > 0 Then
    iFileNum = FreeFile
    Open cdl.filename For Input As iFileNum
    Do While Not EOF(iFileNum)
      Line Input #iFileNum, s
      Printer.Print s
    Close iFileNum
  End If
If you want to format text, you'll have to use the richtextbox

To make selected text bold, underlined, italic or strikethrough you can use the following code:

Statement Action
RichTextBox1.SelBold = True Makes the selected text Bold
RichTextBox1.SelItalic = True Makes the selected text Italic
RichTextBox1.SelUnderline = True Makes the selected text Underlined
RichTextBox1.SelStrikeThru = True Makes the selected text Strikethrough

To change the alignment, simply use this syntax:

RichTextBox1.SelAlignment = Alignment

Where alignment is one of the following constants:

Constant Alignment
vbRight Right
vbLeft Left
vbCenter Centre

So, the following code will set make the selected text underlined, and aligned to the right.

RichTextBox1.SelAlignment = vbRight
RichTextBox1.SelUnderline = True

To print out a formatted richtextbox:

'Name of Rich Text Box Control = rtf
'Write this code under Print Button

rtf.SelPrint Printer.hDC
rtf.SelStart = lastposition
rtf.SelLength = lastSelection
They are right. As the first example load the text into a richTextBox. You can keep the richtextbox hidden if you like so. Then lets say you want to make all numbers Bold. Use a loop to go through each character... then use this:
if isNumeric(charac ) then    'or if charac="important letter"
rtb.SelBold = True
end if

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now