'=======================================================
'- CHANGE LENGTH OF DRAWING TOOLBAR TEXTBOX
'- checks text length & number of chr(10) line breaks
'- very rough - adjust font width/height as required
'=======================================================
Sub CheckLength()
Dim TB As Object
Dim BoxText As String
Dim BoxWidth As Double
Dim FontSize As Integer
Dim CharWidth As Double ' estimated width of each character
Dim CharHeight As Double ' estimated height of each character
Dim CharPerLine As Integer ' characters per line
Dim LineCount As Integer
Dim TextLength As Integer
Dim EOLCount As Integer
Dim EOL As String ' line break character 10
'------------------------------------------------------------
ActiveSheet.Range("A1").Select ' remove focus from text box
EOL = Chr(10) ' end of line character
Set TB = ActiveSheet.Shapes("BC_Comments")
BoxWidth = TB.Width
'------------------------------------------------------------
'- GET TEXTBOX CONTENTS INFORMATION
With TB.TextFrame
BoxText = .Characters.Text
FontSize = .Characters.Font.Size
TextLength = .Characters.Count
If TextLength = 0 Then
MsgBox ("Empty box")
Exit Sub
End If
'----------------------------------------------------------
'- count EOL characters
EOLCount = 0
For c = 1 To Len(BoxText)
If Mid(BoxText, c, 1) = EOL Then
LineCount = LineCount + 1
End If
Next
End With
'--------------------------------------------------------------
'- CALCULATIONS
Select Case FontSize
Case Is = 8
CharWidth = 4.65
CharHeight = 12.77
Case Is = 10
CharWidth = 5.55
CharHeight = 14
Case Is = 12
CharWidth = 5.55
CharHeight = 16.4
Case Else
MsgBox ("Font size " & FontSize & " not valid")
Exit Sub
End Select
'----------------------------------------------
CharPerLine = BoxWidth / CharWidth
LineCount = TextLength / CharPerLine + EOLCount + 1
TB.Height = LineCount * CharHeight
'------------------------------------------------------------
'- testing
' MsgBox ("Box Width : " & BoxWidth & vbCr _
' & "Characters : " & TextLength & vbCr _
' & "FontSize : " & FontSize & vbCr _
' & "LineHeight : " & CharHeight & vbCr _
' & "Per Line : " & CharPerLine & vbCr _
' & "Lines : " & LineCount & vbCr _
' & "Height : " & LineCount * CharHeight)
End Sub