Macro to find string and copy sentence containing string.
Hello,
I need help with a Word macro.
I would like to search a document for a string. If the string is found I want to copy the sentence containing the string to an Excel document.
I have a little experience with Excel and Access VBA but none with the objects and methods used with Word. I would be grateful if someone could at least get me started.
I am using Word 2000.
Thanks.
Finding two or more words simulaneously in a document and copying to excel file
Quote:
Originally Posted by
Aerogal
Greg, I wanted to thank you for the quick response. I've not had a chance to work on my code with your suggestion, but when I do, I'll let you know. And thanks for letting me know why all the "shalls" were found prior to the "wills." I suspected that VB could only search one item at a time with the .Find, but nice to know for certain. My code is in Word VB and I'm exporting the results to an Excel file. However, I do see how to incorporate your logic. Thanks again!
Greg -- Now that I finally have time, I hope you can help me with a little confusion I have. In the original, I see how the logic loops through the array of words to find ("strFind") by splitting up the array and searching each word at a time ("vFind") - if found, copies and pastes accordingly. The VB code works pretty well, too but only finds one word at a time (my conundrum). If you have a moment, could you help me understand the following in your code:
You Split the variable "vFind," but never use the results (why).
In the With loop, you set "orng.Find.Text" to the string variable "strFind" array contents. Walking through execution, the logic is therefore never true for the corresponding "If" statement.
Any help would be greatly appreciated. I can search for 1 word at a time. Would like to search for 2... :think:
Using the wildcard feature to find 2 words simultaneously
Greg, Thank you. I feel very embarrassed, and your response prompted me to learn more about the wildcard feature (word.mvps.org/faqs/general/usingwildcards.htm). It is a lot more powerful than I had known only days ago.
My test document contained upper case words, so my initial macro never ended. I modified the wildcard to be:
Code:
Const strFind As String = "<[SshaWwi]{2,3}ll>"
I tested it on my test document and then a "real" document. Works like a charm. I would have never gotten this far without your help. Thank you so very much.