View Full Version : font size based on character count within a text form field field...
kbishop94
05-10-2024, 11:58 AM
how can I reduce the font size of a text form field based on the number of characters?
The code should make this form field (the one that says "Customer Name") go from this:
31565
to look like this:
31566
I use this code in excel to accomplish the same thing in a userform textbox.. but cant figure out how to do this same thing in word:
    With Me.txtTrainingName
         If .TextLength Mod 4 = 0 Then
             Select Case .TextLength
                 Case Is < 74
                     .Font.Size = 14
                 Case 74 To 80
                     .Font.Size = 13
                 Case 80 To 90
                     .Font.Size = 12
                 Case Is > 90
                     .Font.Size = 11
'
             End Select
         End If
    End With
Aussiebear
05-10-2024, 01:13 PM
Why not just make the font the same size no matter the length of the customer name?
kbishop94
05-10-2024, 01:52 PM
Thank you for replying Aussiebear.
Its because I would like to keep the font size as large as possible (within reason...)
Most entries into most fields (btw I plan to use the code for all the fields on this form) will be short enough not to exceed the default font size.  Only a few will be of a long enough entry that would require a smaller font size that will allow the entry to fit within the confines of particular cell where text form field is.  I wouldn't want to make all the fields smaller than I need to just to allow the occasional long entry to properly fit. 
(this is a protected form btw and the user does not have control to make formatting changes.)  Thanks again.
gmaxey
05-11-2024, 04:51 AM
I have to wonder why (in 2024) you are using formfields instead of content controls in your form.  Regardless:
Sub FFOnExit()
Dim oFld As Word.FormField
  If Selection.FormFields.Count = 1 Then
    Set oFld = ActiveDocument.FormFields(Selection.FormFields(1).Name)
  ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
    Set oFld = ActiveDocument.FormFields(Selection.Bookmarks(Selection.Bookmarks.Count).Na me)
  End If
  With oFld
    Select Case Len(.Result)
       Case Is < 74
         .Range.Font.Size = 14
       Case 74 To 80
         .Range.Font.Size = 13
       Case 80 To 90
         .Range.Font.Size = 12
       Case Is > 90
         .Range.Font.Size = 11
    End Select
  End With
lbl_Exit:
  Exit Sub
End Sub
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.