PDA

View Full Version : Searching sentences with Numbered/Bulleted list



debkev1010
03-05-2008, 08:48 AM
I'm writing some code to search for a certain word in a Word document. When I find the word, I select the sentence and paste it into an excel spreadsheet -


for sentenceCount = 1 to worddoc.sentences.count
doctext = worddoc.sentences.item(sentencecount).text
if instr(1, doctext, "Fred") <> 0 then
activesheet.cells(rowIndex, 1).value = doctext
rowindex = rowindex+1
end if
next

The only problem is when I find "Fred" in a sentence with a colon followed by a bulleted or numbered list, the code above is only selecting up to the colon, leaving out the list when it copies to the excel file. In the Word document after each colon is a carriage return and after each item in the list there's a carriage return. In the Word document I've followed a standard format with these list in that I place a period at the end of the last item in the list (to complete the sentence). Evidently, Word doesn't recognize the list as part of the sentence (when I turn on the paragraph marker, there's a marker next to the colon and a marker at the end of each item in the list). I'm looking for a solution that will either tell Word that the list is part of the preceeding sentence or VBA code that will also select the list with the preceeding sentence.

Thanks,
Kevin

fumei
03-05-2008, 11:38 AM
"Evidently, Word doesn't recognize the list as part of the sentence"

No, it does not, and quite properly. The list is not part of the sentence.

"I'm looking for a solution that will either tell Word that the list is part of the preceeding sentence or VBA code that will also select the list with the preceeding sentence."

You can not do the first for the simple reason as stated above - the list is NOT part of the sentence.

It is possible with VBA, but you will have to very explicitly spec out your logic.

"I've followed a standard format with these list in that I place a period at the end of the last item in the list (to complete the sentence). "

My bolding.

That may be standard typographical format, but Word does not understand conventions of meaning. It understands objects. The list items are terminated by a paragraph mark (carriage return). THAT Word understands. In other words, each listed item IS a paragraph, and IS a sentence. Your code grabs the sentence. The sentence stops at the paragraph mark.

Simply put:

"the code above is only selecting up to the colon" BECAUSE "after each colon is a carriage return"

If you are using Style fully, any VBA code will be much easier. If you are not...then it will be tricky.

debkev1010
03-05-2008, 12:11 PM
Thanks, Gerry,

I've managed to get my VB code to concatenate the sentence and the list into a variable and then I assign the variable to a cell in my spreadsheet. Now, my challenge is to preserve the formatted Numbered List (a, b, c, ...) in Excel.

Thanks again,
Kevin

fumei
03-06-2008, 11:25 AM
Then you should post your code solution.