Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing and margins

Status
Not open for further replies.

SirLars

Technical User
Oct 24, 2000
47
CA
i am a little new to this visual basic and i'm working on a simple little project

i am trying to print a textbox to the printer...
this textbox contains a series of instructions

the problem is that if one of the instructions is too long and there isn't a carriage return in the text the printer doesn't "wrap" the text...

is there a simple way to set the printer margins to 80 chars?

or do i have to "format" the text with a function and look for "carriage returns" within a line of text and insert a carriage return at the space b/w words at/near the 80th character position of each line?

i think it would be so much simpler to just be able to set the left margin to 90 chars...

Lars
 
You are printing just the value of the textbox? So you're wanting no borders around the box, no gray form, etc. to print? I would probably just do a Print Form, and change the textbox to flat, no border, form color white, maybe move the textbox to the upper right hand-corner. Even resize to the width of the form. Then it will print exactly what is on your screen. And after it has printed, have it change it back.
 
Thanks for your response...

yes printform was the first method i was going to use

BUT

the problem would be virtually the same.. the text to be printed is in a text box with scroll bars.. print form will only reproduce the form on the printer..that's why i decided to printer.print txtInstructions.Text

i either need to set printer margins OR write a "text wrap for printer" function to insert a vbCRLF after 80 characters

i was hoping there was a way to set the printer margin as the function seems to sway from the KISS principles

:)
 
I just tried coming up with some code to make a string that added CR's in the right places. It's harder than I thought. To dertine at what character there are empty spaces was the first step. I had those assigned to an array for every character in the string.

So you would need to say, find the closest marked array before 80 (and 160, and 240, etc.) and add a CR. I'm working on some code right now. If I get it done, I'll post it back up.

 
Thanks for the help river guy.. but i got the procedure i needed to format/wordwrap the text

' TEXT BOX PRINT WRAP Procedure for formating to
' fit long text on the printer object
' strTextToWordWrap = string of text from text box To Be Printed.
' LeftMargin = Left Printer Margin Desired.
' RightMargin = Right Printer Margin Desired.

Private Sub TBPrintWrap(ByVal strTextToWordWrap As String, ByVal LeftMargin As Long, ByVal RightMargin As Long)
Dim ICounter As Integer
Dim currWord As String

Printer.CurrentX = LeftMargin
ICounter = 1

Do Until ICounter > Len(strTextToWordWrap)
currWord = ""
Do Until ICounter > Len(strTextToWordWrap) Or Mid$(strTextToWordWrap, ICounter, 1) <= &quot; &quot;
currWord = currWord & Mid$(strTextToWordWrap, ICounter, 1)
ICounter = ICounter + 1
Loop
' check if the word will fit on this line
' if not start a new line
If (Printer.CurrentX + Printer.TextWidth(currWord)) > _
(Printer.ScaleWidth - RightMargin + Printer.ScaleLeft) Then
Printer.Print ' start a new line
Printer.CurrentX = LeftMargin + 300 ' indent wordwrapped line
End If
' word fits so we can print it
Printer.Print currWord;
' now check the length of the next word
Do Until ICounter > Len(strTextToWordWrap) Or _
Mid$(strTextToWordWrap, ICounter, 1) > &quot; &quot;
Select Case Mid$(strTextToWordWrap, ICounter, 1)
Case &quot; &quot; ' is the next character a space?
Printer.Print &quot; &quot;; ' that will fit so print it

Case Chr$(10) ' is the next character a LF ?
Printer.Print ' print a new line
Printer.CurrentX = LeftMargin ' set the next line
' to start printing again
' at the leftmargin

End Select

ICounter = ICounter + 1
Loop ' end the
Loop

End Sub '---------------

sooo to print a textbox you simply call that procedure and send it the left and right margins you want it to have

TBWordWrap mytextbox.text,100,1200

i slightly modified code i found at
it works perfectly for me

Thanks for your help
Lars
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top