extract paragraphes based on a criteria and make a new list.

K

Khoshravan

I have a long list of book names, typed in Word. Whole list is designed in
two line format. Book title appears in first line. In second line there are
some details about book (author, publisher and a school level for which this
book is good for). Book levels are categorized as: elementary, junior high
school and high school. So I have only 3 categories.
I want to extract only those book data related to elementary school. After
extracting (am not sure if this phrase is correct) I want a new list showing
only books good for elementary level.
How can I perform this task?
Thanks in advance
 
J

Jay Freedman

I have a long list of book names, typed in Word. Whole list is designed in
two line format. Book title appears in first line. In second line there are
some details about book (author, publisher and a school level for which this
book is good for). Book levels are categorized as: elementary, junior high
school and high school. So I have only 3 categories.
I want to extract only those book data related to elementary school. After
extracting (am not sure if this phrase is correct) I want a new list showing
only books good for elementary level.
How can I perform this task?
Thanks in advance

I'll describe one way to approach this, and point to some other possibilities.

The first task is to mark up the document so it becomes possible to distinguish
the three categories. One way to do this is to define three different styles --
they can have the same font and paragraph formatting, only the names need to be
different -- and apply the appropriate style to each pair of paragraphs.
(Careful: Word works with paragraphs, not with "lines".)

Although the styles could be applied manually, the length of the document makes
it worthwhile to program a macro to do it. The idea is to use the Find command
to look first for each occurrence of "elementary" and apply the elementary style
to that paragraph and the paragraph before it; then look for "junior high" and
apply the junior high style to that pair of paragraphs; and repeat once more for
"high school". This job could be complicated if the search words also appear in
the book titles. Is there some constant bit of text that could be used to
recognize the search words only in the proper places?

Once the styles are in place, you can display only one category by changing the
Hidden property of that style to True and the Hidden property of the other two
styles to False. Again, that could be done manually, or by a macro; and the
macro could be started by a toolbar button, a MacroButton field, or other means.

Instead of changing the Hidden property of the styles, a macro could copy all
the paragraphs with one style into another document -- that's what I would think
of as "extracting" them.

A different approach to the markup is to insert XML tags around the book
entries, and apply an XSLT transform to select the ones to show.
 
K

Khoshravan

Thanks for your reply
Fortunately search items does not appear in first paragraph.
I understand your whole idea and I think it will answer, however I haven't
write a single Macro in Word and little Macro Knowledge in Excel.
Could you please kindly guide me how to write aforementioned Macro in Word?

Regarding your second approach, the original file does not have the XML
tags. Again, is a Macro in necessary to put it for whole file?

Thanks in advance
 
G

Greg Maxey

Jay,
A different approach to the markup is to insert XML tags around the book
entries, and apply an XSLT transform to select the ones to show.

I won't say that I would love to see you try it ;-), but I would love to see
and to learn how it is done. With the exception of the monkey see, monkey do
sort of stuff that I can cobble together for simple ribbon customization,
XML and particularly the dark voids like XSLT are magical mysteries to me.
I have tried on several occasions to work through some of Bill Coan's
introductory stuff, but I guess it isn't introductory enough for my
particular rock because I just don't get it.
 
J

Jay Freedman

The code is a bit more complicated than it might be, because one of the search
terms is included in another search term.

Sub ApplySchoolStyles()
' assumes the styles named Elementary,
' Junior High, and High exist in the
' current document

Dim srchRg As Range
Dim srchTerms As Variant
Dim Term As Variant

srchTerms = Array("elementary", "high school", "junior high school")
' Note the search order: first any entries that contain "high school"
' (which includes the "junior high school" ones) are given High style.
' Then the "junior high school" search changes those to Junior High
' style.

For Each Term In srchTerms
Set srchRg = ActiveDocument.Range
With srchRg.Find
.Text = Term
While .Execute
srchRg.Expand wdParagraph
srchRg.MoveStart wdParagraph, -1
Select Case Term
Case "elementary"
srchRg.Style = ActiveDocument.Styles("Elementary")
Case "junior high school"
srchRg.Style = ActiveDocument.Styles("Junior High")
Case "high school"
srchRg.Style = ActiveDocument.Styles("High")
End Select
srchRg.Collapse wdCollapseEnd
Wend
End With
Next
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
J

Jay Freedman

I said it could be done, but I didn't say I could do it. <g> I doubt that my
knowledge of XML is as good as yours. Maybe some kind soul will take pity on us
and show us how it's done.
 
J

Janine

Gosh I hope so...

Jay Freedman said:
I said it could be done, but I didn't say I could do it. <g> I doubt that
my
knowledge of XML is as good as yours. Maybe some kind soul will take pity
on us
and show us how it's done.
 
G

Greg Maxey

Jay, Janine

Sounds like we are all in the same boat. We know it can be done but have no
clear idea how. Hopefully someone will heed our collective S.O.S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top