Solved Searching two words in one paragraph

Capitala

Member
Local time
Today, 21:56
Joined
Oct 21, 2021
Messages
91
Hi All,
If I need to search two words (non consecutive) in a paragraph, what to do whether by code of normal find.
Is there any special character to use in search criteria as (^p) ...etc.
Thanks heros
 
This is a 2-step process: first split the text in paragraphs, then loop over the paragraphs and do two finds.
Step 1 can be done with the Split function, splitting on (most likely) vbCrLf, and Step 2 can be done with InStr.
 
I don't usually open WORD to make a macro, I always do this kind of action from Access, so I'm not sure what you would do if you were starting from WORD. However, ....

Once you are in WORD context, either directly or through an app object, you can open the document of interest, then within that document you open the "paragraphs" collection, then (this may be a lot easier to say than to do) select the paragraph you wanted to search, and finally search the paragraph. If you have a way to select a given paragraph by searching the whole document, you can get the paragraph number with this trick:

Code:
Public Function WhichPgh(ByRef wddoc As Word.Document, ByRef WdRng As Word.Range) As Long

    WhichPgh = wddoc.Range(0, WdRng.Paragraphs(1).Range.End).Paragraphs.Count

End Function

Once you have chosen a paragraph to search, you would have to do two FIND operations because the syntax of the FIND object doesn't really consider a case of "either of two targets." Within the paragraph, if you find a specific word number, there is a Words() collection that would give you the position of the word in the paragraph. Again, the trick I showed above for getting a paragraph number ALSO works for getting the number of a word within a paragraph.

This link points you to documentation on the FIND object, which is where you start searching. But you would need to browse around the related topics in this area to see the methods and properties of the FIND object to fully understand it.

 
Well Word has a Record Macro, like Excel, so that should give a basis to start from?
 

Users who are viewing this thread

Back
Top Bottom