View Full Version : Incremental search for Word
alanc
12-11-2008, 09:28 AM
Although not a programmer by training, I have become (somewhat) proficient in VBA over the years through trial and error experimentation, reading, etc. But there is one task that I have always wanted to script but have not been able to figure out.
Is there a way to create an incremental search for Microsoft Word documents? In other words, after invoking the command, begin to type the characters that you want to find. For example, if the cursor is positioned at the start of this message, and I type the "i", the "i" in "training" will be selected. If I then type "c", the two middle letters in the word "proficient" will be selected. If I then type "e", the following word will be selected: ICE.
fumei
12-11-2008, 11:59 AM
No.
I will amend that.
Yes, but it would take a huge amount of work to create, and use a huge amount of resources to function.
Why do you want to do this anyway?
fumei
12-11-2008, 12:10 PM
Let me amend that again, as I actually tried to do it. It is not hard at all. Demo attached.
Note: it does exactly what you asked for. The userform is NOT modal, so you can action in the document. Click "Show My Form" on the top toolbar. You may have to move the userform a bit so you can see.
Type "i", and the "i" in training is selected, add "c" and the "ic" in proficient is selected, add "e" and ice is selected.
However, I wonder about the usefulness of this. The code (below) uses, EACH TIME/CHANGE, the whole document. Is that what you want?
Private Sub TextBox1_Change()
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = TextBox1.Text
.Execute
End With
End Sub
alanc
12-11-2008, 01:40 PM
Wow, This is close to what I had in mind. Obviously, there are gaps in my understanding of VBA! Thank you.
Let me tell you exactly what I am trying to do, and why. Searching is my primary way to navigate around documents, and the usual modal Find is inefficient. There are too many steps. Start a search mode. Enter a search string. Start the search.
With an incremental search, the search begins automatically after each key press, and I never type any more characters than necessary to find what I am looking for.
I implemented an incremental search in my old DOS-based word processor (Borland Sprint), and it saved gobs of time. In the early-1990s, I wrote a free-standing Sprint macro that I continue to use to this day for looking up contacts and dialling their telephone numbers. By virtue of its incremental search, I can usually zero in and dial any number -- there are over 800 in the list -- in a couple of seconds. There are setting changes you can make in Firefox that make it possible (and easy) to select and activate links by typing. In other words, you type to give focus. In some cases, it's significantly faster than pointing and clicking.
Here is what I would want an incremental search in Word to do:
1. Retain the selection after cancelling the seach. For example, if I press Esc or click Cancel, the form closes, and whatever text was selected remains selected.
2. Always search forward from the current selection. If the search string is not found, start searching from the start of the document.
3. If the search string cannot be found in a document, beep, but do not cancel the search.
4. Each time backspace is pressed during the search, the selected text loses its rightmost character. (This makes it easy to continue if you make a typo.)
5. If you start a search, backspace over everything, and then cancel the search, keyboard input focus is at the cursor position of the most recently successful search. E.g., Search for "focus" and backspace 5 times. The cursor is after the "s" in focus.
fumei
12-11-2008, 02:13 PM
You basic premise is flawed.
" Searching is my primary way to navigate around documents"
Bleeeech. There are much better ways to navigate.
As for your list:
1. it does this
2. can be done
3. what? Keep the userform open forever?????
4. huh??? You type "ic" and get the "ic" in proficient selected, backspace and "proficient" in the document becomes "profiient" - no "c"? That does not seem good to me.
5. huh? According to #4 above the cursor would be where the deleted "focus" was.
alanc
12-11-2008, 02:47 PM
"Searching is my primary way to navigate around documents"
Bleeeech. There are much better ways to navigate.
I have injuries that make it difficult to use a mouse, so pointing and clicking are out. Normal keyboard commands for navigating do not provide sufficient fine-grained control. Furthermore, my clients who I develop macros for are mostly blind, have low-vision, or have upper body mobility impairments. What we have in common is a need to navigate quickly, accurately, and with minimal physical and cognitive effort.
As for your list:
1. it does this
Yes, it does.
2. can be done
How? Help... please?
3. what? Keep the userform open forever?????
Nah, just don't stop the search because something is not found. I know of a strangely implemented incremental search that assumes that you want to start from scratch when a search is unsuccesful. I assume a typo.
4. huh??? You type "ic" and get the "ic" in proficient selected, backspace and "proficient" in the document becomes "profiient" - no "c"? That does not seem good to me.
Thank you for a chance to clarify myself. Type "ic" and the "ic" in proficient selected, press backspace, and only the "i" is selected. Press backspace again, and nothing is selected. Close the form, and focus is to the left of the "i." Keyboard focus keeps up with successive successful searches.
fumei
12-11-2008, 03:12 PM
This is why I reiterate again how important it is to be very clear, and very explicit.
I knew what you meant, but I want you to be very very explicit. What you meant was pressing backspace on the userform changes the text on the userform. However, what you wrote was pressing backspace changes "the selected text"...well, the selected text is in the document.
"Nah, just don't stop the search because something is not found. "
Huh? What does "don't stop the search" actually mean????? Be explicit.
There is a userform. It has a textbox. You type text into the textbox. As each character is typed the CURRENT full text of the textbox is searched for in the document. If the current full text in the textbox is not found you want...what to happen?
You say just don't stop the search. Well, if the search string is not found...the search is stopped. So again...what do you mean?
Do you mean (and i am just tossing this out):
clear the textbox to blank
set the start of the next possible at the start of the document
wait
until they either type new text, or close the userform?
What?
I am very very well aware of visual and physical issue with document. My boss is blind (diabetes) and we have a very strong useability department that insists on making things as accessible as possible. For example we have no documentation that uses the word "click". It is always "select".
I use the keyboard a lot, as I prefer it frankly.
alanc
12-12-2008, 07:53 AM
Hi Fumei,
At this point, you have provided enough information to jumpstart my next round of trial and error experimentation. You have introduced me to several VBA techniques that I wasn't familiar with before. Thank you, I appreciate your assistance so far.
I am going to take a stab at refining the incremental search myself. It will be a perfect opportunity for me to add to my modest repertoire of VBA skills.
Stay tuned. I may need more help!
I have an idea for a project. In my work, I create software shortcuts for people with disabilities. Over the years, I have observed that standard "Search" commands are not ideally suited for many of my clients. I would go so far as to say that these commands are overengineered, and are too complicated for the majority of users -- both with and without disabilities. For example, think about the number of steps required to switch directions of the search in Microsoft Word. I have met savvy Word users who did not even realize that it is possible to search backwards.
I want to develop a library of alternative find commands. They may lack the power of the native commands, but are perfect for most searches. I have already developed and field-tested a set of alternative Find commands for Microsoft Word for people who are blind, and another set for people with upper body mobility impairments (which I use myself). I will be publishing a paper on the topic soon.
fumei
12-12-2008, 10:21 AM
I would be interested in anything regarding useability for people with visual or physical disabilities.
I agree with you that some things are over-engineered, and further, there is an emphasis (understandable) on using visual interactivity. In fact, there are some things you must use a mouse for. Although I have worked through most of those.
In my documentation on software for our users I always include the alternative keyboard commands.
As an aside, are you aware that Shift-F10 is the keyboard equivalent to a right-click? This is standard for Windows applications but is rarely documented.
This can help both with users who do not use a mouse, AND for those recording macros. There are places that the macro recording process does not allow a right-click...but it DOES allow the use of Shift-F10.
Quite a lot depends on how your documents are designed and structured in the first place. As we have a number of visually impaired staff (including my boss) I try to build documents with that in mind. It was this that made me delve deeply into the use of bookmarks.
This also encourages the use of proper Styles, as it makes it easier to find things.
Anyway, good luck with your work, and if you come up with anything interesting please post a thread. If you need some help with anything as you go along, also post.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.