Find Events

The file FindEvents.dotm is an event handler that responds to SelectionChange events in Microsoft Word.

You can examine the contents of the template through this report, which was generated by the macro ReportTemplateInfo.

Note: This file is intended to be installed in the Word start-up folder so that the event handler is automatically loaded when Word is launched. The implementation will only work on Windows.

I got interested in pursuing this when a friend said the following in email: "I have always wished that when I search for something, it would come up in the middle of my window, neither way at the top nor way at the bottom. I like to see things in context, and frequently have to scroll up or down a few lines after a search to determine whether the instance I've found is one of the ones I really want. It has annoyed me that Word doesn't automatically position that way."

My first idea about how to solve this problem was to intercept the Find Next button, but as far as I've been able to tell, that's impossible. My next idea was create my own dialog box that looks like the one in Word and use my macro library to implement the search. However, as I was doing some research for that approach, something I read triggered the idea of using events. It was pretty simple to write an event that fired when the selection changed in Word, but I didn't want to do anything when the user just clicked in the window. Eventually I discovered that I could use Windows API calls to detect whether the "Find and Replace" dialog is displayed on the screen. If it is then the Find command must have changed the selection. When the event handler fires after a SelectionChange event and the "Find and Replace" dialog is displayed, the event handler repositions the result of the Find command on the screen so that surrounding content is also shown on the screen. The event handler also ensures that the find result is never obscured by the dialog box.

You might note that this implementation is not quite the "middle of my window" as originally requested by my friend, but she seems happy with the result.

Send me reports of any errors or suggestions you have about my VBA modules.


This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.