you can set the font of your form to the font of your richtextbox, and then measure the width of your text as it would appear on your form
make sure though that you don't print anything on your form anywhere else in your code, as this code will change the font of that as well
Have a look at the following test project :
'1 form with :
' 1 richtextbox control: name=RichTextBox1
' 2 command buttons : name=Command1 name=Command2
Option Explicit
Private Sub Command1_Click()
Dim lngWidth As Long
With RichTextBox1
Font.Name = .Font.Name
Font.Size = .Font.Size
lngWidth = TextWidth(.Text)
End With
Caption = CStr(lngWidth)
End Sub
Private Sub Command2_Click()
With RichTextBox1
.Font.Size = 2 * .Font.Size
End With 'RichTextBox1
End Sub
Private Sub Command3_Click()
With RichTextBox1
.SelFontSize = 2 * .Font.Size
End With 'RichTextBox1
End Sub
Enter some text in the rtb and click on Command1 (the default text should give the value 1005 in the caption of the form
Then click on Command2 which will double the font size and click on Command1 again (the default text would give the value 2010 in the caption of the form
Then select a part of the text in the rtb and click on Command3 which will double the font size of your selection only, then click on Command1 again and see that the value in the caption of the form doesn't change
If you don't allow for different fonts inside your rtb then the code above might work, it will even select the longest line for you and measure that line's width