PDA

View Full Version : Need urgent help with a ‘find a word copy sentence’ macro



smilez
03-28-2019, 11:16 AM
Hi! I’m new to building macros and really need some urgent help with building a macro to do the foll:

*search word doc for a keyword
*then copy the sentence containing that keyword
*then placing the sentence into a 3 column table along with the paragraph heading where the word was found and also the page number. So table should have 1 column with the sentence, 2nd column with the paragraph heading and 3rd column with page #.

Table should be in the same word doc.

Hope someone can help soon, thanks in advance!

macropod
03-28-2019, 01:50 PM
The first hurdle you'll have to overcome is that VBA has no idea what a grammatical sentence is. For example, consider the following:
Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.
For you and me, that would count as one sentence; for VBA it counts as 5 sentences.

The second hurdle is having the code recognise what you mean by 'the paragraph heading', which lacks definition in your problem description.

smilez
03-29-2019, 12:15 PM
Thanks for reviewing! Would it be possible to somehow ‘define’ a sentence as being any string starting with an upper case letter and ending with a period (.)? Not sure how vba works and if this would be possible. This definition would work in my doc as the sentences are fairly simple. Also for paragraph heading those are all bolded and underlined throughout so could that be used to define heading in this scenario? Appreciate your help.


Thanks!

macropod
03-29-2019, 01:55 PM
So with:
I saw Mr. Smith on his way to lunch.
you'd be happy for the result to be either:

I saw Mr.
or:

Smith on his way to lunch.
Somehow I doubt it. And there's all those abbreviations like i.e., e.g., etc. ...

As for your headings, relying on formatting alone just makes things so much harder than they need to be. Anyone ever heard of using heading Styles?

smilez
03-29-2019, 02:07 PM
Well I only mentioned those ‘features’ that could be used because I know for sure my doc has nothing of the sorts such as Mr., Ms. or any abbreviated words. This would be the easiest way to identify the sentences in my doc. In regards to the heading do you have any suggestions as to how this could be identified in a vba?

macropod
03-29-2019, 02:28 PM
Well I only mentioned those ‘features’ that could be used because I know for sure my doc has nothing of the sorts such as Mr., Ms. or any abbreviated words.
I suggest you make an attitude adjustment. We're not mind readers here and you didn't bother to describe the content in a way anyone would understand what you're now telling us.


Getting 'simple' sentences is trivial. For example:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, StrOut As String
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = InputBox("What is the Text to Find")
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
i = i + 1
StrOut = StrOut & vbCr & .Sentences.First.Text
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
StrOut = Replace(StrOut, vbCr & vbCr, vbCr)
Application.ScreenUpdating = True
MsgBox i & " instances found:" & StrOut
End Sub
A search will turn up various threads for converting such output to a table. What's more problematic though, it finding headings of the kind you've described. Basically, that means nesting another 'Find' within the above to look backwards through the document to the first preceding instance of a paragraph heading whose content is all bold and underlined. If you'd used heading Styles, the approach could be much simpler. See, for example: http://www.vbaexpress.com/forum/showthread.php?64884-Using-a-string-in-a-line-of-code

smilez
03-29-2019, 02:59 PM
Hm, attitude adjustment?? See I could’ve said the same in response to your previous post but hey I figured everyone’s interpretation is different right? It all boils down to how the msg is read n interpreted by the reader n just to clarify I responded with not an ounce of attitude, was just on the run at the time n wanted to respond ASAP. Wouldn’t make sense since I’m the one looking for help believe me! Lol

Anyhow, thanks again n I appreciate your efforts.