PDA

View Full Version : [SLEEPER:] Dialog box for EditFind



kevinj
03-07-2013, 12:09 PM
Hi - my first time posting on this forum. I am using Word 2010.

I am trying to write a macro which, at its completion, activates another document and then comes up with the EditFind dialog box (i.e., that would perform the exact same thing as if you were to type "Alt-E, F" in Word. The following is what I came up with:



Windows("AnotherDocument.docx").Activate
With Dialogs(wdDialogEditFind)
.WholeWord = True
.Display
End With


Apparently I don't know how to work properly with dialog boxes because this does not work ideally. The dialog box that comes up does the following:

Displays the message "Word found no items matching these criteria" even though no text to search has been entered.
Requires the user to carry forward the search clicking on "Find Next" or pressing "Alt-F", but doesn't allow the user to simply press "Enter."
After searching, comes up with a VBA error message if text is searched for and not found.

I'd appreciate your help. Thanks!

Doug Robbins
03-08-2013, 06:15 AM
It would probably be better if you described exactly what it is that you want to do. Using the built in dialogs, such as wdDialogEditFind, is not always the best way to do something from VBA.

Also note that using


Windows("AnotherDocument.docx").Activate

could be unpredictable and it is better to declare a Document object variable that you set the the document upon which you want the action to take place.

kevinj
03-08-2013, 01:35 PM
It's hard to describe in much simpler terms what I am trying to do.

Basically, I'm in a particular document in Word. I want to be able to enter a shortcut key (such as Ctrl-Y) which pulls up another active document, and then pull up the search dialog specifying whole words to be searched only. Then I can enter the search term desired and press Enter. That's it. This may seem too simple a task for a macro, but due to the extremely repetitive nature of the task I am performing, it would be very useful.

Your note on Windows Activate not being reliable is well taken.

Thanks.

fumei
03-08-2013, 02:16 PM
I am not sure I would call using Activate unreliable (or "unpredictable"). I rarely use it myself as I prefer to action documents directly using document objects.

I am not getting the errors you seem to be getting.

Doug Robbins
03-08-2013, 06:52 PM
It wasn't the Activate that I was suggesting was unpredictable but using
Windows("AnotherDocument.docx") and I agree it is far better to use document objects.

Here, running




With Dialogs(wdDialogEditFind)
.WholeWord = True
.Display
End With


Does cause the message "Word found no items matching these criteria" to be displayed in the Find dialog and Pressing Enter, even though the Find Next has the focus does not work. That is the case whether .Display or .Show is used.

For what Kevinj wants to do, I would use an InputBox to get from the user the word that they to find in code such as:


Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = InputBox("Enter the word that you want to find.", "Find")
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

fumei
03-08-2013, 11:14 PM
If you are going to do that it would be much better to not use Selection, but a range object of the document.

kevinj
03-11-2013, 12:28 PM
Doug and Fumei,

Thanks for your contributions. I'll probably go with code similar to Doug suggested (unless someone comes up with a way to use wdDialogEditFind).

Kevin

kevinj
03-11-2013, 01:06 PM
I tried Doug's suggestion. It works fine as far as it goes. However, I have a special problem in that I want to be able to type in special characters such as é and ō. The input box can't handle those characters (neither can the built-in Navigation find feature), which is one reason I was trying to use the dialog box.

Also, the method doesn't handle repeated finds - I suppose the code could be altered to handle this.

I hate to have anyone spend to much time on answering this, especially since it's just a short-term requirements I have. Thanks for the answers received again.

Doug Robbins
03-11-2013, 02:19 PM
To insert é into and InputBox, hold down the Alt key and type 0233 on the numeric keypad. Then insert õ use 0245.

fumei
03-11-2013, 04:00 PM
"which is one reason I was trying to use the dialog box." Really, you never mentioned it before.

kevinj
03-13-2013, 09:20 AM
Well, I still haven't given up completely on this. I tried two other commands:


Application.Run macroname:="EditFind"

and


CommandBars.FindControl(ID:=141).Execute

The "Application.Run macroname:" command works in Word 2007, but in Word 2010 it works just like the faulty "Dialogs(wdDialogEditFind)" command. (My guess is that one will also work in 2007, but I haven't tried it.) Why would these commands not work in 2010 as they did in 2007?

The "CommandBars.FindControl" command works to bring up the Advanced Find dialog box - fine, but I don't know how to pre-program preferences (such as WholeWord:=True).

Fumei - you were right. I forgot to mention the requirement to put in special characters - sorry.

Doug - thanks for your correct suggestion to use Alt + numeric pad for special characters. Another thing I didn't explain (sorry again) was that I am using shortcut keys for the special characters (such as Ctrl-a for á, Ctrl-Shift-a for ā, etc.) for about 12 special characters, so using Alt+ numbers would be cumbersome.

Thanks.

Doug Robbins
03-13-2013, 03:31 PM
Maybe you need to take a step backwards and advise exactly what it is that you want done with the new document (rather than concentrate on the way in which you have tried, which does not seem to lead in the right direction).

There may be other ways of achieving it.

fumei
03-13-2013, 09:09 PM
Sorry, but

"the faulty "Dialogs(wdDialogEditFind)" command"

is incorrect. The dialog is NOT faulty. Your assumptions are.

kevinj
03-15-2013, 12:53 PM
Fumei,

Doug said it didn't work either (see his March 8, 7:52 post).

Just curious - did you test it on Word 2010 or an earlier version of Word? For me it does work on earlier versions of Word but not Word 2010. It's the Word 2010 "Dialogs(wdDialogEditFind)" that doesn't seem to work.

Thanks.

fumei
03-15-2013, 07:56 PM
Actually he said: "Using the built in dialogs, such as wdDialogEditFind, is not always the best way to do something from VBA. "

which is not the same as saying it does not work. And even if it does not work in 2010, that also does not mean it is "faulty". MS chganged a bunch of things in 2010.

kevinj
03-18-2013, 09:02 AM
Thanks.

fumei
03-26-2013, 05:45 PM
So you give up and just go someplace else. Cross-posted.

You are not going to get the functionality you want. Use a userform as Doug suggested.

macropod
03-27-2013, 02:23 AM
Cross-posted at: http://www.msofficeforums.com/word-vba/16406-using-editfind-vba-word-2010-a.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

kevinj
04-02-2013, 08:55 AM
First, I apologize for the cross-posting. That was inconsiderate of me, as I was enlightened by the cross-posting message to see. I'll avoid doing that again.

Second, although I couldn't find why the EditFind dialog box wouldn't work, I found a workaround and am sharing it here in case it of use to anyone else. It involves the CommandBars.FindControl command:



Windows("My Document.docx").Activate
With Selection.Find
.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
CommandBars.FindControl(ID:=141).Execute


Again, I hope this might be useful to someone else, and again, I apologize for the cross-posting.

kevinj
04-02-2013, 09:10 AM
On top of my other apologies, I formatted the last post wrong somehow and couldn't edit it (insufficient permissions or something). Here's another try:

First, I apologize for the cross-posting. That was inconsiderate of me, as I was enlightened by the cross-posting message to see. I'll avoid doing that again.

Second, although I couldn't find why the EditFind dialog box wouldn't work, I found a workaround and am sharing it here in case it of use to anyone else. It involves the CommandBars.FindControl command:



Windows("My Document.docx").Activate
With Selection.Find
.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
CommandBars.FindControl(ID:=141).Execute


Again, I hope this might be useful to someone else, and again, I apologize for the cross-posting.

fumei
04-02-2013, 12:40 PM
How on earth does that work for what you have asked for. I do not see that it allows your special characters, nor the multiple use of the Enter key.

In fact, as is, that does not even search for any specific text.

In fact, this is not something new, as you ALREADY posted using FindControl.


CommandBars.FindControl(ID:=141).Execute


The "CommandBars.FindControl" command works to bring up the Advanced Find dialog box - fine, but I don't know how to pre-program preferences (such as WholeWord:=True).

kevinj
04-03-2013, 08:50 AM
For me, this works as advertised and meets my needs. I also tested it in Word 2003 and it works in that version too.


The simple test:

Create a new Word document with text that can be searched in it, and save it as "My Document.docx".
Open a new blank document and create a macro with the code in my previous post.
Run the macro.
The EditFind dialog box comes up in "My Document."
Type in the text to be searched and press "Enter" as normal for the EditFind dialog box. Continue to press "Enter" to find further occurrences of the text.As far as special characters, as mentioned before, I have assigned keyboard shortcuts to these characters using "Insert, Symbol, More Symbols, Shortcut Key." Then when using these shortcut keys while the EditFind dialog box is displayed, the special characters are inserted into the "Find What" line. These shortcut keys do NOT work in the Navigation Find feature, which is one reason I wanted to use the EditFind dialog.

The "Whole Word" or other parameters desired can be established in the "With Selection.Find" code preceding the CommandBars.FindControl, which is what I missed the first time using that command.

Hope that brings it back down to earth!