Results 1 to 16 of 16

Thread: Printing The Text In A RichTextBox

  1. #1

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Printing The Text In A RichTextBox

    How Do I Do It

    And So That The User Can Choose The Printer

    Thanx

    Lavarock09

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing The Text In A RichTextBox

    This is the preferred method of choosing a printer.

    http://vbforums.com/attachment.php?attachmentid=37738

    I use a WYSIWYG form to print a rtb as a preview pane before printing it. Google, and you will get the M$ site that explains it.

  3. #3

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing The Text In A RichTextBox

    i got to this site

    http://support.microsoft.com/kb/q146022/

    and i have implimented this, but i cant choose the printer

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing The Text In A RichTextBox

    The program that I posted lets you select the printer that will be used until your program ends.

  5. #5

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing The Text In A RichTextBox

    but i want it so that the printer dialog box comes up and you can choose a printer and it will print what is in the richtextbox

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing The Text In A RichTextBox

    Hmmm. Maybe I posted the wrong example. Try this one.

    EDIT: Removing duplicate code.

    http://vbforums.com/attachment.php?attachmentid=41070
    Last edited by dglienna; Jan 30th, 2006 at 10:22 PM.

  7. #7

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing The Text In A RichTextBox

    this is the code for my command button for printing

    VB Code:
    1. Private Sub Command3_Click()
    2.   Dim printDlg As PrinterDlg
    3. Set printDlg = New PrinterDlg
    4. ' Set the starting information for the dialog box based on the current
    5. ' printer settings.
    6. printDlg.PrinterName = Printer.DeviceName
    7. printDlg.DriverName = Printer.DriverName
    8. printDlg.Port = Printer.Port
    9.  
    10. ' Set the default PaperBin so that a valid value is returned even
    11. ' in the Cancel case.
    12. printDlg.PaperBin = Printer.PaperBin
    13.  
    14. ' Set the flags for the PrinterDlg object using the same flags as in the
    15. ' common dialog control. The structure starts with VBPrinterConstants.
    16. printDlg.Flags = VBPrinterConstants.cdlPDNoSelection _
    17.                  Or VBPrinterConstants.cdlPDNoPageNums _
    18.                  Or VBPrinterConstants.cdlPDReturnDC
    19. Printer.TrackDefault = False
    20.  
    21. ' When CancelError is set to True the ShowPrinterDlg will return error
    22. ' 32755. You can handle the error to know when the Cancel button was
    23. ' clicked. Enable this by uncommenting the lines prefixed with "'**".
    24. '**printDlg.CancelError = True
    25.  
    26. ' Add error handling for Cancel.
    27. '**On Error GoTo Cancel
    28. If Not printDlg.ShowPrinter(Me.hWnd) Then
    29.     Debug.Print "Cancel Selected"
    30.     Exit Sub
    31. End If
    32.  
    33. 'Turn off Error Handling for Cancel.
    34. '**On Error GoTo 0
    35. Dim NewPrinterName As String
    36. Dim objPrinter As Printer
    37. Dim strsetting As String
    38.  
    39. ' Locate the printer that the user selected in the Printers collection.
    40. NewPrinterName = UCase$(printDlg.PrinterName)
    41. If Printer.DeviceName <> NewPrinterName Then
    42.     For Each objPrinter In Printers
    43.        If UCase$(objPrinter.DeviceName) = NewPrinterName Then
    44.             Set Printer = objPrinter
    45.        End If
    46.     Next
    47. End If
    48.  
    49. ' Copy user input from the dialog box to the properties of the selected printer.
    50. Printer.Copies = printDlg.Copies
    51. Printer.Orientation = printDlg.Orientation
    52. Printer.ColorMode = printDlg.ColorMode
    53. Printer.Duplex = printDlg.Duplex
    54. Printer.PaperBin = printDlg.PaperBin
    55. Printer.PaperSize = printDlg.PaperSize
    56. Printer.PrintQuality = printDlg.PrintQuality
    57.  
    58. ' Display the results in the immediate (Debug) window.
    59. ' NOTE: Supported values for PaperBin and Size are printer specific. Some
    60. ' common defaults are defined in the Win32 SDK in MSDN and in Visual Basic.
    61. ' Print quality is the number of dots per inch.
    62. With Printer
    63.     Debug.Print .DeviceName
    64.     If .Orientation = 1 Then
    65.         strsetting = "Portrait. "
    66.     Else
    67.         strsetting = "Landscape. "
    68.     End If
    69.     Debug.Print "Copies = " & .Copies, "Orientation = " & _
    70.        strsetting
    71.     If .ColorMode = 1 Then
    72.         strsetting = "Black and White. "
    73.     Else
    74.         strsetting = "Color. "
    75.     End If
    76.     Debug.Print "ColorMode = " & strsetting
    77.     If .Duplex = 1 Then
    78.         strsetting = "None. "
    79.     ElseIf .Duplex = 2 Then
    80.         strsetting = "Horizontal/Long Edge. "
    81.     ElseIf .Duplex = 3 Then
    82.         strsetting = "Vertical/Short Edge. "
    83.     Else
    84.         strsetting = "Unknown. "
    85.     End If
    86.     Debug.Print "Duplex = " & strsetting
    87.     Debug.Print "PaperBin = " & .PaperBin
    88.     Debug.Print "PaperSize = " & .PaperSize
    89.     Debug.Print "PrintQuality = " & .PrintQuality
    90.     If (printDlg.Flags And VBPrinterConstants.cdlPDPrintToFile) = _
    91.        VBPrinterConstants.cdlPDPrintToFile Then
    92.          Debug.Print "Print to File Selected"
    93.     Else
    94.          Debug.Print "Print to File Not Selected"
    95.     End If
    96.     Debug.Print "hDC = " & printDlg.hdc
    97. End With
    98. Exit Sub
    99. '**Cancel:
    100. '**If Err.Number = 32755 Then
    101. '**    Debug.Print "Cancel Selected"
    102. '**Else
    103. '**    Debug.Print "A nonCancel Error Occured - "; Err.Number
    104. '**End If
    105.  
    106.    End Sub

    how do i make it print the richtextbox's text

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing The Text In A RichTextBox

    It was in the code:


    VB Code:
    1. rtb.SelPrint printDlg.hDC

    with rtb being the name of my rich text box. I ran it before I posted, and it worked.

  9. #9

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing The Text In A RichTextBox

    i can't see it in the code on here

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing The Text In A RichTextBox

    Sorry, I thought that was the latest. I'll rebuild the zip file.


    Hmm. It got smaller, so I don't know what was in it. Then new one has replaced it.
    Last edited by dglienna; Jun 30th, 2005 at 04:15 PM.

  11. #11

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing The Text In A RichTextBox

    thanx, it works fine apart from 1 thing, how do I put a gap around the side so it doesn't print right to the edge

  12. #12
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Printing The Text In A RichTextBox

    Try the .RightMargin property or maybe .SelIndent and .SelRightIndent.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  13. #13

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing The Text In A RichTextBox

    i want it all round the richtextbox

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing The Text In A RichTextBox

    Try this

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   Dim margin As Integer
    5.   margin = 600
    6.   With rtb
    7.      .SelStart = 1
    8.      .SelLength = Len(.Text)
    9.     .SelIndent = margin
    10.     .SelRightIndent = margin
    11.   End With
    12. End Sub

  15. #15

    Thread Starter
    Lively Member lavarock09's Avatar
    Join Date
    Jun 2005
    Posts
    124

    Re: Printing The Text In A RichTextBox

    so with that, when i print, there will be a space around the page

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Printing The Text In A RichTextBox

    Yes, the margin is in twips, I think. I just changed them until the text appeared indented properly. I went from 6 to 60 to 600. It depends how wide your rtb is.

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