printing from textbox

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 1310 Views - Last Post: 04 February 2012 - 09:17 AM Rate Topic: -----

Topic Sponsor:

#1 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

printing from textbox

Posted 21 January 2012 - 03:18 PM

this is a test code i tried to use for printing from a textbox:


Private Sub Command1_Click()
CD.PrinterDefault = True
CD.CancelError = True
CD.Flags = cdlPDReturnDC + cdlPDNoPageNums
If Text1.SelLength = 0 Then
CD.Flags = CD.Flags + cdlPDAllPages
Else
CD.Flags = CD.Flags + cdlPDSelection
End If

On Error Resume Next
CD.ShowPrinter
If Err Then
Exit Sub
End If

Printer.Print Text1.Text
End Sub



however it does not work.
i do not get any error message but the printer dosent print anything.
a little help will be nice :)

Is This A Good Question/Topic? 0
  • +

Replies To: printing from textbox

#2 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 21 January 2012 - 04:19 PM

fixed the problem of making it print but now the text go off the page >.<
Was This Post Helpful? 0
  • +
  • -

#3 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 99
  • Joined: 02-January 12

Re: printing from textbox

Posted 21 January 2012 - 04:56 PM

You need to write code to break your textbox text into lines and then print each line individually.
Printer.Print just prints one line of text, it doesn't care how long it is.
Was This Post Helpful? 0
  • +
  • -

#4 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 99
  • Joined: 02-January 12

Re: printing from textbox

Posted 21 January 2012 - 05:10 PM

Here's some crappy code i wrote for a similar problem:

lengthOfText = Printer.TextWidth(RTrim$(textbox.text))
                            
    If lengthOfText > 430 Then
        textToPrint$ = RTrim$(textbox.text)
        Call WRAP_TEXT(Printer, 430, textToPrint$, linesOfText, returnedTextLines())
        
        For b = 1 To linesOfText
          Printer.Print returnedTextLines(B)

        Next b
        
    Else
        Printer.Print RTrim$(textToPrint$);
    End If

'''------------------------------------------------------


Public Sub WRAP_TEXT(prnt, textLength, text$, linesOfText, textLines() As String)

text$ = text$ + " "
start = 1
lengthOfText = Len(text$)

   prevSpace = spac
   spac = InStr(spac + 1, text$, " ")
   If spac = 0 Then
      If Len(tx$) > 0 Then
         linesOfText = linesOfText + 1
         textLines(linesOfText) = Mid$(text$, start, lengthOfText - (start - 1))
         'Debug.Print textLines(linesOfText)
      End If
      Exit Do
   End If
   
   tx$ = Mid$(text$, start, spac - start)
   
   widthText = prnt.TextWidth(tx$)
   If widthText > textLength Then
      linesOfText = linesOfText + 1
      textLines(linesOfText) = Mid$(text$, start, prevSpace - start)
      start = prevSpace + 1
      
   End If
     
Loop

End Sub




Note the Printers scalemode is set to 2 (which is points).
so the text length is 430 points

This post has been edited by deanobravo: 21 January 2012 - 05:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 22 January 2012 - 08:17 AM

>< i dont understad what the scale mean, it replace the fontsize?
also this code have so many bugs i cant run it ><
the returnedTextLine is missing and the loop is broken...
how do i fix it? ><

another thing: what if i reached the end of the page? how i start another page?
Was This Post Helpful? 0
  • +
  • -

#6 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 22 January 2012 - 10:36 AM

>.<

i guess the steps i should take are these:
1) set certian text length.
2)check if every line is not bigger then the predefined length, if it does break it into 2 lines and check them again.
3)store all of the lines inside array type string.
4)print the array 1 element at a time.

now how do i know if the text i have is bigger then a single page and how i make new page?
Was This Post Helpful? 0
  • +
  • -

#7 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 99
  • Joined: 02-January 12

Re: printing from textbox

Posted 22 January 2012 - 01:15 PM

The scalemode just sets how you measure the width and height of the page e.g.
you can use millimetres(=6), inches, points(=2), twips and a few others.
I would inlude a line counter in your code so that when it reaches a certain amount (say 50, bit of trial and error required) you do a Printer.Newpage and reset the counter to zero. If you are doing a lot of print testing I recommend downloading cutePDF Printer it will save you a lot of paper and ink/toner.
Was This Post Helpful? 0
  • +
  • -

#8 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 99
  • Joined: 02-January 12

Re: printing from textbox

Posted 22 January 2012 - 01:25 PM

Sorry, the "Do" is missing from the loop, not sure what happened when copy/pasted.
At work at the moment so can't help much.
Was This Post Helpful? 0
  • +
  • -

#9 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 99
  • Joined: 02-January 12

Re: printing from textbox

Posted 22 January 2012 - 09:48 PM

View Postdeanobravo, on 22 January 2012 - 12:10 PM, said:

Here's some crappy code i wrote for a similar problem:

Printer.Scalemode = 6
lengthOfText = Printer.TextWidth(RTrim$(textbox.text))
                            
    If lengthOfText > 170 Then
        textToPrint$ = RTrim$(textbox.text)
        Call WRAP_TEXT(Printer, 170, textToPrint$, linesOfText, returnedTextLines())
        
        For b = 1 To linesOfText
          Printer.Print returnedTextLines(B)

        Next b
        
    Else
        Printer.Print RTrim$(textToPrint$);
    End If

'''------------------------------------------------------


Public Sub WRAP_TEXT(prnt, textLength, text$, linesOfText, textLines() As String)

text$ = text$ + " "
start = 1
lengthOfText = Len(text$)

Do

   prevSpace = spac
   spac = InStr(spac + 1, text$, " ")
   If spac = 0 Then
      If Len(tx$) > 0 Then
         linesOfText = linesOfText + 1
         textLines(linesOfText) = Mid$(text$, start, lengthOfText - (start - 1))
         'Debug.Print textLines(linesOfText)
      End If
      Exit Do
   End If
   
   tx$ = Mid$(text$, start, spac - start)
   
   widthText = prnt.TextWidth(tx$)
   If widthText > textLength Then
      linesOfText = linesOfText + 1
      textLines(linesOfText) = Mid$(text$, start, prevSpace - start)
      start = prevSpace + 1
      
   End If
     
Loop

End Sub




Note the Printers scalemode is set to 6 (which is mm).
so the maximum text length is 170mm


I have made a couple of changes to the above code so it should work better

This post has been edited by deanobravo: 22 January 2012 - 09:51 PM

Was This Post Helpful? 0
  • +
  • -

#10 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 23 January 2012 - 09:43 AM

by the way what the returnedTextLines() mean?
is it the first part of the code you posted or is it a separate sub/function?
Was This Post Helpful? 0
  • +
  • -

#11 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 99
  • Joined: 02-January 12

Re: printing from textbox

Posted 23 January 2012 - 11:59 AM

It is the array that returns the lines of text to print from the sub.
Also the scalemode is very important as the default is twips which is a very small measurement and if you set textlength to 170 you probably won't even fit one word in that length.

This post has been edited by deanobravo: 23 January 2012 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#12 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 23 January 2012 - 12:29 PM

this is how i try to use the code you gave me:

Option Explicit

Dim LengthOfText As Long, linesoftext As Long

Dim start As Long, prevSpace As Long, spac As Long, tx$

Dim textToPrint$, b As Long, widthText As Variant

Dim returnedTextLines() As String

Private Sub Command1_Click()
Printer.ScaleMode = 6
LengthOfText = Printer.TextWidth(RTrim$(TextBox.text))
                            
    If LengthOfText > 170 Then
        textToPrint$ = RTrim$(TextBox.text)
        Call WRAP_TEXT(Printer, 170, textToPrint$, linesoftext, returnedTextLines())
        
        For b = 1 To linesoftext
          Printer.Print returnedTextLines(B)

        Next b
        
    Else
        Printer.Print RTrim$(textToPrint$);
    End If

End Sub


'---------------------------------------------------------------------------------


Public Sub WRAP_TEXT(prnt, textLength, text$, linesoftext, textLines() As String)

text$ = text$ + " "
start = 1
LengthOfText = Len(text$)

Do

   prevSpace = spac
   spac = InStr(spac + 1, text$, " ")
   If spac = 0 Then
      If Len(tx$) > 0 Then
         linesoftext = linesoftext + 1
         textLines(linesoftext) = Mid$(text$, start, LengthOfText - (start - 1))
         Debug.Print textLines(linesoftext)
      End If
      Exit Do
   End If
   
   tx$ = Mid$(text$, start, spac - start)
   
   widthText = prnt.TextWidth(tx$)
   If widthText > textLength Then
      linesoftext = linesoftext + 1
      textLines(linesoftext) = Mid$(text$, start, prevSpace - start)
      start = prevSpace + 1
      
   End If
     
Loop


Printer.EndDoc
End Sub



however if the text seems to be too big the program will crash at line
textLines(linesoftext) = Mid$(text$, start, prevSpace - start)


i get runtime error '9' subscript out of range
Was This Post Helpful? 0
  • +
  • -

#13 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 23 January 2012 - 01:58 PM

managed to solve the above problem by redim the variable but now the printer only print part of the text, not all of it
Was This Post Helpful? 0
  • +
  • -

#14 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 99
  • Joined: 02-January 12

Re: printing from textbox

Posted 23 January 2012 - 11:10 PM

I've only ever used this to wrap 3 or 4 lines of text.
I do recall that it will do strange things if the text has carriage returns/linefeeds included.
Was This Post Helpful? 0
  • +
  • -

#15 Neku  Icon User is online

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 174
  • Joined: 21-May 09

Re: printing from textbox

Posted 24 January 2012 - 09:42 AM

i decided a different tactic:
the program will make a temp text file holding the text i want to print.
it will then open it few times and calculate how many lines there are and put them in dynamic string array.
and then will print it
i think it would work but might be ugly code xD
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3