Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Word VBA Textbox searching

Posted on 2011-10-05
5
Medium Priority
?
356 Views
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?

Thanks!
     JRA
0
Comment
Question by:JohnRobinAllen
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 1000 total points
ID: 36921265
You need to set .Selstart and .Selstart as in this very simple example
Private Sub CommandButton1_Click()

    Dim x As Long
    
    With TextBox1
        .SetFocus
        x = InStr(1, .Value, "...")
        If x > 0 Then
            .SelStart = x - 1
            .SelLength = 3
        End If
    End With


End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36921271
Sorry make that .SelStart and .SelLength
0
 

Author Comment

by:JohnRobinAllen
ID: 36921296
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.
     JRA
0
 

Author Comment

by:JohnRobinAllen
ID: 36924473
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, "...")

to

     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.
     JRA
0
 

Author Closing Comment

by:JohnRobinAllen
ID: 36924492
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.
     JRA
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Suggested Courses

564 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