How do you limit the number of words (not characters) in a ActiveX text box?

Susan Singer 0 Reputation points
2023-07-17T21:14:46.2+00:00

I have inserted an ActiveX control text box in a Word document want to limit the number of words (not characters).

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Word | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AllenXu-MSFT 24,951 Reputation points Moderator
    2023-07-18T02:17:56.53+00:00

    There is no built-in word limit property for ActiveX text boxes in Word documents. However, you can write VBA code to achieve this. Here is an example:

    1. Insert an ActiveX textbox into your Word document.
    2. Right-click the textbox and select "View Code" to open the VBA editor.
    3. Paste the following code into the editor:
    Private Sub TextBox1_Change()
        Dim wordCount As Integer
        wordCount = Len(Trim(TextBox1.Value)) - Len(Replace(Trim(TextBox1.Value), " ", "")) + 1
        If wordCount > 10 Then 'replace 10 with the desired word limit
            Dim startIndex As Integer
            startIndex = InStrRev(TextBox1.Value, " ", 10)
            MsgBox "You have reached the maximum word limit of 10", vbInformation, "Word Limit Exceeded"
            TextBox1.Value = Left(TextBox1.Value, startIndex)
        End If
    End Sub
    

    Note: In the above code, change "TextBox1" to match the name of your ActiveX textbox.

    This code will limit the number of words to 10. Replace "10" in the code with the desired word limit. When the user types in the textbox and enters more than the specified number of words, a message box will appear and further text input will be prevented.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.