MS Office Forum / Word / Programming / October 2005
Find Dialogue Box
|
|
Thread rating:  |
SF - 24 Oct 2005 04:53 GMT I want to find various words in a document using the following method: I will copy a word to my clipboard, and then: Create a macro to take that word from the clipboard and find it in the text of a Word document.
Here's my problem:
When I double click the REC macro button and then paste the selected word into the "Find" dialogue box, that word gets permanently embedded into the macro code!
So if I stop the macro, then manually select another word, and copy it to the clipboard, and then run the macro . . . . it goes and finds the old word! Because that word is in the macro code----regardless of what I copy to the clipboard next!
I sure hope this makes sense. More importantly, I sure hope someone understands what I mean. (More importantly, still, I sure hope someone can give me an answer . . . .
:-) I think what I need is a variable, MyString As String Then copy the selected text into MyString Then set:
With Selection.Find .Text = MyString
But I tried this and it doesn't work. I think the variable (MyString) has to be converted to something in quotes.
SF
Doug Robbins - Word MVP - 24 Oct 2005 05:14 GMT Use:
Dim wdtoFind As String wdtoFind = Selection.Text Selection.HomeKey wdStory Selection.Find.ClearFormatting With Selection.Find Do While .Execute(FindText:=wdtoFind, MatchWildcards:=False, Wrap:=wdFindStop, Forward:=True) = True MsgBox "Found " & wdtoFind & "." Loop End With
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
>I want to find various words in a document using the following method: > I will copy a word to my clipboard, and then: [quoted text clipped - 30 lines] > > SF SF - 24 Oct 2005 15:12 GMT Thank you, Doug, but it didn't work.
It threw me into a "loop of death". Every time I hit okay on the message box, it went and found another instance of whatever it was looking for (which wasn't what was on the clipboard, by the way). I couldn't get out of the loop. Pressing ESC didn't work and I could not click on anything because the only window I had access to with the cursor was the msg box. And it wouldn't go away. Kept popping right back up. One good thing: every time it popped up, it had selected another instance of the search item. The first time I ran it, it was the letter "A", and the next time it was a paragraph mark. I haven't tried it again.
Reboot fixed everything, though.
> Use: > [quoted text clipped - 43 lines] >> >> SF Doug Robbins - Word MVP - 24 Oct 2005 20:43 GMT What do you want to do when you find the word?
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Thank you, Doug, but it didn't work. > [quoted text clipped - 58 lines] >>> >>> SF SF - 25 Oct 2005 02:27 GMT Hey! I'm glad you asked.
Let me explain the whole thing. Maybe you can suggest a method better than the way I'm going about it.
WindowsXP SP2 Word 2000
I wrote a 400-page novel (95,000 words). But I did not apply any styles to the document. All text was directly formatted: Chapter headings Epigraphs Subheadings Time stamps Etc.
So when I learned what Styles were and how they work, I made a copy of the document and did a select all and Ctrl + Spacebar. Then I went through it and put in Styles for all the above-mentioned items.
Now everything is nicely Styled . . . . . except for the italicized words!
There are hundreds and hundreds of words in the original document that are italicized. (For example: "The couple crossing the street sensed that the approaching car was <purposely> trying to run them down!" Where the word <purposely> is in italics.
So I want to open the old doc and the new doc. Then search in the old doc for an italicized word or phrase and copy it to the clipboard. Then I want to make the new doc the ActiveWindow and search for the plain text that matches the italics text in the old doc. Then I want to set the new text to italics.
I can do it manually, one word or phrase at a time by the above method but each word takes a couple dozen clicks, and I saw real quick how that would not be a good idea for a few hundred words.
Oh, and one more thing . . . . .
In the above example the word <purposely> is not an extremely common word, so the search would probably find the correct instance of the word in the new doc. But take the case in the following example:
"No, not this one, I want <that> one."
If there is a lot of intervening text, then the search may find half a dozen "that"s before coming to the correct instance of "that". So I can't make the macro do a "Replace All". I have to click the macro for each one. And if it finds the incorrect instance of the string, that's fine with me. I'll just ask it to search again until it comes to the right place.
Sure hope I didn't wear you out reading all this. (But like I said, glad you asked. . . . :-)
SF
> What do you want to do when you find the word? > [quoted text clipped - 60 lines] >>>> >>>> SF Doug Robbins - Word MVP - 25 Oct 2005 05:39 GMT Yes, you have created yourself a bit of a chore. The code I gave you would allow you to find all instances of a selected word in the same document, though it can be changed to handle the word being on the clipboard and even the swapping from one document to the other. However, the chore part comes from not all instances of the Word needing to be changed. This means that you are going to have to be presented with message box for each instance of the found word asking if you want it to be italicsed or not and I am not sure that will be much easier than doing it by using the Edit Replace dialog manually
But here's the code for it (Note, after copying the word to the clipboard, you will need to switch to the document to which you want to apply the formatting before running the macro.)
Dim MyData As DataObject Dim wdtoFind As String Dim Response
Set MyData = New DataObject MyData.GetFromClipboard wdtoFind = MyData.GetText
Selection.HomeKey wdStory Selection.Find.ClearFormatting With Selection.Find Do While .Execute(FindText:=wdtoFind, MatchWildcards:=False, _ Wrap:=wdFindStop, Forward:=True) = True Response = MsgBox("Do you want to italicise this instance of " & wdtoFind, vbYesNo) If Response = vbYes Then Selection.Font.Italic = True End If Loop End With
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Hey! I'm glad you asked. > [quoted text clipped - 121 lines] >>>>> >>>>> SF SF - 26 Oct 2005 07:41 GMT I put Your macro inside a simple sub, i.e.:
Sub ChangeToItalics() (pasted your code) End Sub
I ran the macro, but I get a compile error that says "User defined type not defined" and the first line of code is highlighted: Dim MyData As DataObject
Do I need something more?
> Yes, you have created yourself a bit of a chore. The code I gave you > would allow you to find all instances of a selected word in the same [quoted text clipped - 157 lines] >>>>>> >>>>>> SF Tony Jollans - 26 Oct 2005 12:35 GMT You need to add a reference to the MS Forms 2.0 object library
-- Enjoy, Tony
> I put Your macro inside a simple sub, i.e.: > [quoted text clipped - 194 lines] > >>>>>> > >>>>>> SF SF - 27 Oct 2005 17:53 GMT Ooops, that's a little over my head. I don't know what a reference is, nor a Forms object library for that matter. Am I in over my head?
> You need to add a reference to the MS Forms 2.0 object library > [quoted text clipped - 225 lines] >> >>>>>> >> >>>>>> SF Doug Robbins - Word MVP - 27 Oct 2005 19:43 GMT In the Visual Basic Editor, select References from the Tools menu and scroll down through the list until you come across the "Microsoft Forms 2.0 Object Library" and the check the box in front of it.
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Ooops, that's a little over my head. > I don't know what a reference is, nor a Forms object library for that [quoted text clipped - 237 lines] >>> >>>>>> >>> >>>>>> SF SF - 28 Oct 2005 11:03 GMT I don't have "Microsoft Forms 2.0 Object Library" in the list!!
> In the Visual Basic Editor, select References from the Tools menu and > scroll down through the list until you come across the "Microsoft Forms [quoted text clipped - 247 lines] >>>> >>>>>> >>>> >>>>>> SF Greg - 28 Oct 2005 12:45 GMT Just add a userform to your project (you don't have to use it) and the forms library will then be available in the references.
SF - 28 Oct 2005 14:06 GMT Okay. Thanks Greg. (Good Heaven's, you're up early. . .. :-)
> Just add a userform to your project (you don't have to use it) and the > forms library will then be available in the references. SF - 28 Oct 2005 17:06 GMT Okay, Doug, one last problem, I think. I can tell we're almost there.
Sometimes the macro puts me in an endless loop. If I say "No" that's not the word I want to italicize, then it will find the next instance of that word. Okay so far. Then, if I say "Yes", the word gets italicized, but I don't drop out of the loop. It just goes to the next instance of the same word and asks again.
> In the Visual Basic Editor, select References from the Tools menu and > scroll down through the list until you come across the "Microsoft Forms [quoted text clipped - 247 lines] >>>> >>>>>> >>>> >>>>>> SF Doug Robbins - Word MVP - 28 Oct 2005 18:20 GMT That is why I said the following in my response of 10/25:
Yes, you have created yourself a bit of a chore. The code I gave you would allow you to find all instances of a selected word in the same document, though it can be changed to handle the word being on the clipboard and even the swapping from one document to the other. However, the chore part comes from not all instances of the Word needing to be changed. This means that you are going to have to be presented with message box for each instance of the found word asking if you want it to be italicsed or not and I am not sure that will be much easier than doing it by using the Edit Replace dialog manually
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Okay, Doug, one last problem, I think. > I can tell we're almost there. [quoted text clipped - 265 lines] >>>>> >>>>>> >>>>> >>>>>> SF SF - 28 Oct 2005 19:38 GMT Let me explain a little more fully.
I open both documents, doc1 and doc2 I create a macro that will find italics and paste the text to the clipboard then activate doc2 window. I insert your code next (except declarations which I put at the top). I run the macro. It finds the first instance of italics in doc1 and takes me to the same word in doc2 then displays msg box. I say "Yes". The word gets italicized in doc2 and the macro stops.
Perfect! That's exactly what I want.
But, in another trial, if I say "No" to that first message box, it goes to the second instance. So far so good, . . . but then. . . .if I say "Yes" to that second message box, the word gets italicized, but then the macro doesn't stop! It keeps going whether I say "Yes" or "No" and keeps finding the next instance. And I can't make it stop. I get that little "error" beep whenever I click on anything else. It only recognizes the "Yes" or "No". The only way out is to re-boot.
All I want is for the macro to stop and hold its place after each run, no matter whether I say yes, or no! Then All I have to do is click the macro button again and it will find the next instance. In fact I really don't need the message box at all. Clicking the macro button to proceed to the next instance of the word is pretty much the same thing as answerying no. Then, when the correct search word is found and selected in the second document, I'll just click the Italicize button. Botta-Bing!
I tried fiddling with the code to avoid the loop, but I just screwed everything up. I don't know enough about this stuff.
Please help, I'm not trying to be difficult. Really.
> Okay, Doug, one last problem, I think. > I can tell we're almost there. [quoted text clipped - 265 lines] >>>>> >>>>>> >>>>> >>>>>> SF Doug Robbins - Word MVP - 28 Oct 2005 21:27 GMT That is the way it was designed to work. What if there is more than one instance of the Word that you want to italicise?
In the case where you said "yes" there must have only been one instance of that Word in the document.
If you only want the oppotunity to italicise one instance of the word, then use:
Dim MyData As DataObject Dim wdtoFind As String Dim Response
Set MyData = New DataObject MyData.GetFromClipboard wdtoFind = MyData.GetText
Selection.HomeKey wdStory Selection.Find.ClearFormatting With Selection.Find Do While .Execute(FindText:=wdtoFind, MatchWildcards:=False, _ Wrap:=wdFindStop, Forward:=True) = True Response = MsgBox("Do you want to italicise this instance of " & wdtoFind, vbYesNo) If Response = vbYes Then Selection.Font.Italic = True Exit Do End If Loop End With
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Let me explain a little more fully. > [quoted text clipped - 311 lines] >>>>>> >>>>>> >>>>>> >>>>>> SF SF - 29 Oct 2005 15:03 GMT Thanks, Doug.
Your macro works just fine!
I am one happy camper.
> That is the way it was designed to work. What if there is more than one > instance of the Word that you want to italicise? [quoted text clipped - 346 lines] >>>>>>> >>>>>> >>>>>>> >>>>>> SF
|
|
|