Word VBA Textbox searching

Posted on 2011-10-05
Last Modified: 2012-05-12
Using VBA with MS Word, I have a form with a textbox that contains some words with an ellipsis (". . .") in the text, for example as in
       "Chocolate is . . . drink."

I need to select the ellipsis so that when the user starts to type, his words will replace the ellipsis.  

I can use SetFocus to put the cursor in the textbox, but is there any easy (or hard) way I can get it to search for and select the ". . ." string?

Question by:JohnRobinAllen
    LVL 17

    Accepted Solution

    You need to set .Selstart and .Selstart as in this very simple example
    Private Sub CommandButton1_Click()
        Dim x As Long
        With TextBox1
            x = InStr(1, .Value, "...")
            If x > 0 Then
                .SelStart = x - 1
                .SelLength = 3
            End If
        End With
    End Sub

    Open in new window

    LVL 17

    Expert Comment

    Sorry make that .SelStart and .SelLength

    Author Comment

    Thanks very, very much. I am sure your solution is correct, but I cannot test it until tomorrow morning. I have to leave right now but will be back in about fourteen hours.

    Author Comment

    andrewssd3's second comment makes no sense in that the routine already had those two words.

    The first suggested solution was almost perfect, and when I changed the seventh line in it from
         x = InStr(1, .Value, "...")


         x = InStr(.Text, ". . .")

    it worked perfectly. The initial "1" as an argument in the InStr is redundant, so I removed it.
    Note that since ellipses should have spaces between the dots, I put them in the string and changed the line 10 to
        .SelLength = 5

    A hearty, big thanks for such an easy and elegant solution.

    Author Closing Comment

    This is a most useful routine. My comments are explained elsewhere in the answers. The solution needed some tweaking, but the changes were minimal to make this a great contribution to our databank of solutions.
         Many many thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
    There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
    This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
    Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now