CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Printer Object

  1. #1
    Join Date
    Mar 2001
    Location
    Limerick, Ireland
    Posts
    8

    Printer Object

    Hello, I am trying to learn visual basic 5. My boss has asked me to write a text editor which I have done, but I am having problems with printing.
    I have used a text box for my text area and used the printer object to print but the text at the right of the screen will not print.
    the code I have used to print is:

    texteditor.dialogbox.ShowPrinter
    Printer.Print texteditor.textwindow.Text
    Printer.EndDoc

    any help would be much appreciated.

    John F.


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Printer Object

    With the printer object you will need to be sure that all things fit on a line, if not, you will have to cut down the line yourself or the end will be lost. This is code that does this

    public Sub PrintThis(strText as string)

    Dim strToBePrinted as string
    Dim PWidth as Long
    Dim FFile as Integer
    Dim strLine as string

    PWidth = Printer.ScaleWidth

    ' write text to file
    ' much easier to seperate line from eachother
    FFile = FreeFile
    Open App.Path & "\prt.tmp" for Output as #FFile
    print #FFile, strText
    Close #FFile

    ' open the file just created
    FFile = FreeFile
    Open App.Path & "\prt.tmp" for input as #FFile

    Do Until EOF(FFile)
    ' get every line, one by one
    Line input #FFile, strLine
    strLine = strLine & " " & vbCrLf
    Do While Printer.TextWidth(strLine) > PWidth
    ' line to long to fit page
    Dim strLineTmp as string
    Dim iPos1 as Long
    Dim iPos2 as Long
    iPos1 = InStr(strLine, " ")
    strLineTmp = Left(strLine, iPos1 - 1)
    iPos2 = InStr(iPos1 + 1, strLine, " ")
    ' build the line word by word, until it won't fit anymore
    Do While Printer.TextWidth(strLineTmp & mid(strLine, iPos1, iPos2 - iPos1)) <= PWidth
    iPos1 = iPos2
    iPos2 = InStr(iPos1 + 1, strLine, " ")
    If iPos2 <> 0 then
    strLineTmp = strLineTmp & mid(strLine, iPos1, iPos2 - iPos1)
    else
    Exit Do
    End If
    Loop
    strToBePrinted = strToBePrinted & strLineTmp & vbCrLf
    strLine = mid(strLine, iPos1 + 1)
    Loop
    strToBePrinted = strToBePrinted & strLine
    Loop

    ' we got what we need, so send it to the printer
    Printer.print strToBePrinted
    Printer.EndDoc

    End Sub




    Tom Cannaerts
    [email protected]

    The best way to escape a problem, is to solve it.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Mar 2001
    Location
    Limerick, Ireland
    Posts
    8

    Re: Printer Object

    Thanks very much, you are a life saver.

    John F.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured