Friday, January 04, 2008

Mapping "Find Next/Previous" to F3/Shift+F3 in MS Word

Earlier today, after years of using Microsoft Word, I finally decided that I'd had enough of reopening the Find dialog every time I want to do a "Find Next" or "Find Previous" – that is, to have Word search up or down from the current caret position to find the next (or previous) instance of the string that I had most recently searched for via the Find dialog.

Visual Studio comes with this functionality by default, mapped to F3 for Find Next and Shift+F3 for Find Previous. Some other applications including Firefox and Notepad2 (Florian Balmer's excellent lightweight Notepad-like text editor) support these keyboard shortcuts as well. (Regular old notepad.exe supports F3, but not Shift+F3.) I'm very accustomed to using F3/Shift+F3, so I set out to get my copy of Word to support these.

(Update 4/28/2009: Jeff Cogswell commented with a much better solution to this problem that doesn't involve writing a custom macro, as detailed in this post -- see the comments on this post below for his solution!)

A search revealed that Microsoft Word supports a "Find Again" function, mapped by default to Shift+F4 and to Ctrl+Alt+y, but that isn't what I was looking for; I wanted to be able to easily search in either direction without having to open up the Find dialog and alter the setting of the search direction option.

So I ended up writing a couple of simple VBA functions using Word 2003's integrated Visual Basic Editor to do what I was looking for:

Sub FindNext()
  
   DoFind True

End Sub

Sub FindPrevious() DoFind False End Sub
Sub DoFind(findDirection As Boolean) 'Save the initial Find direction in order to restore it when we're done, 'so that we don't alter the current "Find Up" setting in the regular Find dialog. Dim initialFindDirection As Boolean initialFindDirection = Selection.Find.Forward 'Do the Find in the requested direction. Selection.Find.Forward = findDirection Selection.Find.Execute 'Restore the initial Find direction. Selection.Find.Forward = initialFindDirection End Sub

I determined that the Selection.Find.Execute statement was the key to getting Word to repeat a Find operation by using the Tools | Macro | Record New Macro functionality to record a macro of the built-in Ctrl+Alt+y "Find Again" shortcut, and then inspecting the resulting generated code in the VBA editor. Using the editor's intellisense functionality, it was simple to determine that the Selection.Find.Forward property was what controls the direction of the Find.

At first, I just wrote the FindNext and FindPrevious methods, setting Selection.Find.Forward to the appropriate value, and then calling Selection.Find.Execute. When testing those methods, though, I noticed that the setting of the Find direction carried over to be the new initial value for the Find dialog; i.e. after running the FindPrevious method, the next time the Find dialog was used, it would be set to find in the Up direction by default. I added the DoFind method to handle this issue, by temporarily setting Find.Forward to the necessary value, doing the Find, and then setting Find.Forward back to its original value.

To use these FindNext and FindPrevious functions yourself in Word 2003, paste the code above into the VBA editor (Tools | Macro | Visual Basic Editor), and save them. Then, map the F3 and Shift+F3 keys (or other keys of your choice) to FindNext and FindPrevious respectively by using the Customize Keyboard dialog (Tools | Customize, then click the Keyboard button at the bottom of the Customize dialog).

I would imagine that these functions should be similarly usable under Word 2007 as well, but since 2007's menu system and customization interface have changed, there are likely to be some changes to the steps necessary to enter the functions and map them to the F3 and Shift+F3 keys.

16 comments:

  1. Very ususable function...thanks.

    ReplyDelete
  2. Hi! I was searching for the answer to this same problem and I found your blog. But I decided to do some more research before adding the macros. I know that this functionality exists by clicking the little double-arrow icons in the lower-right of the Word window, under the scrollbars. They key was to find the name of the commands.

    So here's what I did. Floating the mouse over the two double-arrows, I saw a little tooltip appear, telling me the keystrokes Ctrl+Page Up and Ctrl+Page Down were assigned to these commands.

    I then opened up the Keyboard assignment window, and clicked just any random command and tried assigning the keystroke Ctrl+PageDown and saw the message:

    "Currently assigned to: BrowseNext"

    That must be the name. So I selected category "All Commands", scrolled down and found BrowseNext and BrowsePrev. There they were. I assigned F3 to one and Shift+F3 to the other.

    Done! :-) (Of course, you would think Microsoft would have made the commands a little more obvious...)

    --Jeff

    ReplyDelete
  3. p.s. I should add that I'm using Word 2007. I'm not sure if these commands were available in earlier versions.

    Jeff

    ReplyDelete
  4. Jeff, great solution! That's a lot better than my solution (since it's simpler and doesn't involve needing to set up any macros). I've confirmed that it works under Word 2003. Thanks very much for sharing this information!

    Funny that I never even noticed those little blue arrows under the vertical scrollbar at the lower-right corner of the Word window. They're there in Word 2003, too!

    ReplyDelete
  5. How do I access this "Keyboard assignment window" you speak of?

    ReplyDelete
  6. @Anonymous: It's the "Customize Keyboard" dialog. In Word 2003, you can get there from Tools menu | Customize, and then in the Customize dialog that appears, press the Keyboard button.

    I'm not sure about Word 2007. If you're using 2007 and can't figure out how to find the Customize Keyboard dialog, let me know, and I can check when I get home. (I have access to Office 2003 at work, and 2007 at home.)

    ReplyDelete
  7. BrowseNext and BrowsePrev depend on the object type selected for browse. Be sure the object type is "Find".

    ReplyDelete
  8. I'm delighted that I have observed this weblog. Finally anything not a junk, which we go through incredibly frequently. The website is lovingly serviced and kept up to date. So it need to be, thank you for sharing this with us.

    ReplyDelete
  9. Thanks so much for this, Jeff Cogswell/Jon Schneider.
    This has been bugging me for so many years. Finally I got fed up and found this page.


    In office 2010:
    File -> Options -> Customize Ribbon -> "Customize"-button
    Scroll down in "Categories"-window and select "All Commands"

    In "Commands" window scroll down to:
    BrowseNext
    BrowsePrev

    and assign shortcut keys F3 and F3+shift

    Heh, funnily enough it seems like F3 isn't assigned to anyting in Word 2010.
    Just about every program I've ever used has F3 as "Find next", so I find this really weird. Even notepad and wordpad use F3 this way.

    ReplyDelete
  10. Microsoft have had a real penchant these recent years of breaking well-established standards and habits, for no apparent reason...

    Grrr.

    Thanks for finding the totally obscure solution Mr Cogswell. I had searched (pun not intended) for a solution to this a few times and failed...

    (No FindNext??? Oh, it's now called "BrowseNext", of course! - Good grief!)

    ReplyDelete
  11. I had the same issue, and I am glad to say assigning the browse next/prev to f3/shift f3 works find in word 2010 as well.

    ReplyDelete
  12. THANKS A LOT. MICROSOFT PURPOSEFULLY DESTROYS SOFTWARE AND SELLS IT AS "NEW VERSION" AS A MONEY MAKING RACKET.

    Thanks for the Macros. I am very frustrated with the same problem, so I will try your macros.



    I am so disgusted with Microsoft, I will try never ever buy their software again. The practice of destroying software and disabling features and selling it as "new version" or "upgrade" is a racket just to make money.

    ReplyDelete
  13. To put this into context: Microsoft has a document called Windows User Experience Interaction Guidelines - http://msdn.microsoft.com/en-us/library/windows/desktop/aa511258.aspx
    This document describes best practices as proposed by Microsoft. It includes the following (excerpt):
    The following shortcuts can be used by any program, but they must have the given meaning:
    (...)
    F3 Find next.

    There are tons of features in MS Office annoyingly not practical - but fortunately, this time there is still a hidden chance of customizing it.

    ReplyDelete
  14. Hi, brilliant solution thanks to the author as well as those who posted useful comments. Just to highlight the fact that 'Shift+F3' is NOT free/unassigned in Word 2010, it's assigned by default to "Toggle Case". However 'F3' is free/available.

    Anyway, many thanks for good post.

    ReplyDelete
  15. Thanks for the help !
    I'm a developer using every often F3 or n (like in vi).


    I never had find the BrowseNext and BrowsePrevious on word before using word2008... you save my day ^^.
    (By the way, 12 hours before the end of the word ^^)

    Cheers !
    kéké from France !

    ReplyDelete
  16. F3 is assigned to inserting a saved quickpart. You type in the first 3-4 letters and press F3 to insert whatever you named that quickpart. For example if you saved this text "The quick brown fox jumps over the fence" and named it BrownFox. You can go into a new document and type in Bro and press F3 to quickly insert that quickpart.

    ReplyDelete

Non-spammers: Thanks for visiting! Please go ahead and leave a comment; I read them all!

Attention SPAMMERS: I review all comments before they get posted, and I REPORT 100% of spam comments to Google as spam! Why not avoid getting your account banned as quickly -- and save us both a little time -- by skipping this comment form and moving on to the next one on your list? Thanks, and I hope you have a great day!