Results 1 to 9 of 9

Thread: Printing Bold

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    Printing Bold

    HI All,

    I have this function that sets the contents of a Rich text box to bold and sets some of the text red, What I want to know is how can I print the contents of the Rich text box as it is seen in my program (Bold and red)? as at the moment it prints it all out as normal text.


    VB Code:
    1. Private Sub Show_Failed_Conversions()
    2.     Dim currentnumber As Long
    3.     Dim newnum As Integer
    4.     Dim oldnum As Integer
    5.     Dim FindBracket As Integer
    6.     'Add time and data to the log file
    7.     frmQConversion.txtLogFails.Text = "Time: " & Format$(Now, "hh:mm:ss") & vbCrLf & "Date: " & Format$(Now, "dd/mm/yy") & vbCrLf & vbCrLf
    8.    
    9.     'Loop though the collection and add items to the log file
    10.     For currentnumber = 1 To holder.Count Step 1
    11.        
    12.         frmQConversion.txtLogFails.Text = frmQConversion.txtLogFails.Text & holder(currentnumber) & holdName(currentnumber) & " to " & HoldMe(currentnumber) & vbCrLf
    13.         frmQConversion.txtLogFails.SelStart = 0
    14.         frmQConversion.txtLogFails.SelLength = Len(frmQConversion.txtLogFails.Text)
    15.         frmQConversion.txtLogFails.SelBold = True
    16.     Next currentnumber
    17.         newnum = 1
    18.         oldnum = 2
    19.         For FindBracket = 1 To Len(frmQConversion.txtLogFails.Text) Step 2
    20.             oldnum = InStr(oldnum, frmQConversion.txtLogFails.Text, "[")
    21.             newnum = InStr(newnum, frmQConversion.txtLogFails.Text, "]")
    22.                 If oldnum = 0 Then
    23.                     Exit For
    24.                 Else
    25.             frmQConversion.txtLogFails.SelStart = oldnum - 1
    26.             frmQConversion.txtLogFails.SelLength = (newnum - oldnum + 1)
    27.             frmQConversion.txtLogFails.SelBold = True
    28.             frmQConversion.txtLogFails.SelColor = vbRed
    29.             oldnum = newnum + 1
    30.             newnum = newnum + 2
    31.                 End If
    32.         Next FindBracket
    33.        
    34.     'Clear the collection
    35.     Set holder = Nothing
    36.     Set holdName = Nothing
    37.     Set HoldMe = Nothing
    38. End Sub

    And here is where im printing it.

    VB Code:
    1. Private Sub cmbPrintLog_Click()
    2.     Printer.Print txtLogFails.Text
    3.     Printer.EndDoc
    4. End Sub

    Thanks

    Loftty

  2. #2
    use a richtextbox and in that amke text bold .... i dont know if that wuld work

  3. #3
    Lively Member
    Join Date
    Jan 2003
    Posts
    79
    Hi mate,
    I think the problem is that you are saving to a .txt file which will not preserve the formatting from the RichTextBox. Try saving as .rtf or .doc and print those.

    All the best,

    Dave.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598
    Hi Dave,

    I'm not saving the file all I want to do is print the contents of the Rich text box.

    Loftty

  5. #5
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969
    I saw this on the web. I have not tried it:

    VB Code:
    1. ' #VBIDEUtils#*********************************************
    2. ' * Programmer Name  : Waty Thierry
    3. ' * Web Site         : [url]www.geocities.com/ResearchTriangle/6311/[/url]
    4. ' * E-Mail           : [email][email protected][/email]
    5. ' * Date             : 28/06/99
    6. ' * Time             : 13:01
    7. ' ************************************************************
    8. ' * Comments         : Print RichTextBox contents
    9. ' *
    10. ' *
    11. ' ***************************************************************
    12.  
    13. Private Declare Function SendMessage Lib "user32" Alias _
    14. "SendMessageA" (ByVal hWnd As Long, ByVal msg As Long, _
    15. ByVal wp As Long, lp As Any) As Long
    16.  
    17. Private Declare Function GetDeviceCaps Lib "gdi32" _
    18.   (ByVal hdc As Long, ByVal nIndex As Long) As Long
    19.  
    20. Private Const WM_USER = &H400
    21. Private Const EM_FORMATRANGE As Long = WM_USER + 57
    22.  
    23. Private Const PHYSICALOFFSETX As Long = 112
    24. Private Const PHYSICALOFFSETY As Long = 113
    25.  
    26. Private Type RECT
    27.    Left           As Long
    28.    Top            As Long
    29.    Right          As Long
    30.    Bottom         As Long
    31. End Type
    32.  
    33. Private Type CharRange
    34.    cpMin          As Long
    35.    cpMax          As Long
    36. End Type
    37.  
    38. Private Type FormatRange
    39.    hdc            As Long
    40.    hdcTarget      As Long
    41.    rc             As RECT
    42.    rcPage         As RECT
    43.    chrg           As CharRange
    44. End Type


    VB Code:
    1. Public Function PrintRTF(rtf As RichTextBox, nnLeftMarginWidth _
    2.  As Long, nnTopMarginHeight As Long, nnRightMarginWidth As _
    3.  Long, nnBottomMarginHeight As Long) As Boolean
    4.  
    5. ' #VBIDEUtils#************************************************
    6. ' * Programmer Name  : Waty Thierry
    7. ' * Web Site         : [url]www.geocities.com/ResearchTriangle/6311/[/url]
    8. ' * E-Mail           : [email][email protected][/email]
    9. ' * Date             : 30/10/98
    10. ' * Time             : 14:43
    11. ' * Module Name      : Main_Module
    12. ' * Module Filename  : Main.bas
    13. ' * Procedure Name   : PrintRTF
    14. ' * Parameters       :
    15. ' *                    rtf As RichTextBox
    16. ' *                    nnLeftMarginWidth As Long
    17. ' *                    nnTopMarginHeight As Long
    18. ' *                    nnRightMarginWidth As Long
    19. ' *                    nnBottomMarginHeight As Long
    20. ' ***************************************************************
    21. ' * Comments         :
    22. ' *
    23. ' *
    24. ' *************************************************************
    25. On Error GoTo ErrorHandler
    26. Dim nLeftOffset      As Long
    27. Dim nTopOffset       As Long
    28. Dim nLeftMargin      As Long
    29. Dim nTopMargin       As Long
    30. Dim nRightMargin     As Long
    31. Dim nBottomMargin    As Long
    32. Dim fr               As FormatRange
    33. Dim rcDrawTo         As RECT
    34. Dim rcPage           As RECT
    35. Dim nTextLength      As Long
    36. Dim nNextCharPos     As Long
    37. Dim nRet             As Long
    38.  
    39. Printer.Print Space(1)
    40. Printer.ScaleMode = vbTwips
    41. nLeftOffset = Printer.ScaleX(GetDeviceCaps(Printer.hdc, _
    42.    PHYSICALOFFSETX), vbPixels, vbTwips)
    43.    
    44. nTopOffset = Printer.ScaleY(GetDeviceCaps(Printer.hdc, _
    45.    PHYSICALOFFSETY), vbPixels, vbTwips)
    46.    
    47. nLeftMargin = nnLeftMarginWidth - nLeftOffset
    48. nTopMargin = nnTopMarginHeight - nTopOffset
    49. nRightMargin = (Printer.Width - nnRightMarginWidth) _
    50.    - nLeftOffset
    51.    
    52. nBottomMargin = (Printer.Height - nnBottomMarginHeight) _
    53.    - nTopOffset
    54.    
    55. rcPage.Left = 0
    56. rcPage.Top = 0
    57. rcPage.Right = Printer.ScaleWidth
    58. rcPage.Bottom = Printer.ScaleHeight
    59. rcDrawTo.Left = nLeftMargin
    60. rcDrawTo.Top = nTopMargin
    61. rcDrawTo.Right = nRightMargin
    62. rcDrawTo.Bottom = nBottomMargin
    63. fr.hdc = Printer.hdc
    64. fr.hdcTarget = Printer.hdc
    65. fr.rc = rcDrawTo
    66. fr.rcPage = rcPage
    67. fr.chrg.cpMin = 0
    68. fr.chrg.cpMax = -1
    69. nTextLength = Len(rtf.Text)
    70.  
    71. Do
    72.    fr.hdc = Printer.hdc
    73.    fr.hdcTarget = Printer.hdc
    74.    nNextCharPos = SendMessage(rtf.hWnd, EM_FORMATRANGE, _
    75.      True, fr)
    76.    If nNextCharPos >= nTextLength Then Exit Do
    77.    fr.chrg.cpMin = nNextCharPos
    78.    Printer.NewPage
    79.    Printer.Print Space(1)
    80. Loop
    81.  
    82. Printer.EndDoc
    83. nRet = SendMessage(rtf.hWnd, EM_FORMATRANGE, _
    84.   False, ByVal CLng(0))
    85.  
    86. PrintRTF = True
    87.  
    88. Exit Function
    89. ErrorHandler:
    90.     PrintRTF = False
    91. End Function

  6. #6
    Lively Member
    Join Date
    Jan 2003
    Posts
    79
    Hi loftty,
    Sorry mate (note to self: Learn to read the code properly before posting).
    Have you tried using the .selprint method of the richtextbox?

    VB Code:
    1. RichTextBox1.SelPrint (Printer.hDC)

    I've only had a quick go with it but it seems to preserve the formatting.

    All the best,

    Dave.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598
    Hi Dave,

    It does print it out bold but it only prints out the first line, then when I hit print again it prints the whole RTF. what would be wrong?

    Thanks

    Loftty

  8. #8
    Lively Member
    Join Date
    Jan 2003
    Posts
    79
    Weird!!!

    Can you re-post the code?

    Dave.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598
    Hi Dave,

    Here is the code where I print

    VB Code:
    1. Private Sub cmbPrintLog_Click()
    2.  
    3.     txtLogFails.SelPrint (Printer.hDC)
    4.  
    5. End Sub

    The other code is in the first message.

    Thanks

    Loftty

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