Log in

View Full Version : Word 2007 - Find and select all text until...



llyamah
03-12-2012, 10:56 AM
Hello All

Before you reply I'd like to point out that in previous posts I've been informed that I have breached forum etiquette (posting the same question in other forums, poor title). I'd like to apologise for any offence caused. Please bear with me since I am quite new to this. Thank you for any help that you can offer.

How would I modify the below code so that, rather than stopping when the word 'Italy' is found, it stops when any string that fulfils certain conditions is found. The conditions are:

1) It finds some more text that is underlined; or
2) It finds some text that is not black; or
3) It finds one of five particular phrases

Also, I will be looping this so that it goes through the rest of the document to find other instances of 'Ireland' that are underlined and then selecting the text below until the three conditions are met. Esentially I am looking to have everything in the document that relates to 'Ireland' highlighted, ready to copy and paste into a new document.

Thanks again, here is the macro:

Sub Ireland()
'
' Ireland Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Font.Underline = wdUnderlineSingle
With Selection.Find
.Text = "Ireland"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
Selection.Find.Font.Underline = wdUnderlineSingle
With Selection.Find
.Text = "Italy"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
End With
Selection.Find.Execute
End Sub

akuini
03-15-2012, 11:37 AM
Can you give a sample of your document ( use the attachment), just use fake data but with the same layout.


3) It finds one of five particular phrases

What are the five particular phrases?

llyamah
03-15-2012, 01:52 PM
Hi akuini

Thanks so much for the reply, much appreciated.

I will create some samples and send to you. I did do this in another thread, but I don't think I did a very good job of it and so didn't quite get what I was hoping for. I'm going to go away and create a couple of really decent sample documents.

Unfortunately I cannot tell you the exact phrases, as this in itself could give out some information which, whilst not sensitive, would give away the company that I am working for which I'd rather not do. Suffice it to say that the headings do not feature in the text that I am looking to extract.

I'll create a couple of sample documents now and hopefully all will be clear. I'll probably create a new thread, because I think it might be better to start again and I'll try to be clear with what I want to do.

llyamah
03-15-2012, 03:28 PM
Decided not to create a new thread, as I think I've created enough already. Here are the sample documents.

The 'regional' document is the document that I want to split up, and the 'Ireland' document is the document that I want to produce by running a macro. Obviously I want to be able to do this for any country that I choose.

Some stuff to explain:

- Highlights don't need to be included in the country specific reports because it is just a repeat of what is elsewhere in the report.

- The reason why the 'Alcoholic Beverages' section has 'region' and 'country' as sub-headings is so that you can easily navigate this with the hyperlinks - because this section is usually the longest part of the report, we wanted 'country' to appear so that we could click through to all of the country stuff.

- In my earlier post, when I say that I want it to search until it finds a particular phrase, here it would be 'red meat dishes', 'pork dishes', 'shellfish dishes' or 'other fish dishes'. Some of the individual words in these phrases may appear in the text that I want to extract, but the phrase itself will not appear in the text.

- Where no text for a particular country appears underneath one of the headings/sub-headings, it ought to display 'no information to report'.

- The header needs to change to reflect the country that is being reported.

- Ideally, rather than collapsing everything else, when creating a country report it will open a new document so that I can quickly go back to the regional report and produce a new country report. Obviously this will involve copying the header over etc.

Someone has pointed out to me on another thread that a similar sample document is not very suited to the sort of extraction that I want to do - I don't know whether this has changed now that I have created a slightly different sample report.

Hopefully now you see why I want it to copy until one of the three conditions are met, and continue this throughout the document....

Thanks for any help that you can offer - even if just advice on how to learn to do this myself!

EDIT: Because I can only upload one document, I've had to do this all in one file, so the country report is on the end.

akuini
03-16-2012, 02:16 AM
Hi, llyamah
I've examined your sample document.
I have some questions:

The non-black font, is it only set to the product names (Non-alcoholic Beverages, Meat and Fish,Shellfish Dishes, etc) as a beginning of each 'section' ? and is it only one color?



In my earlier post, when I say that I want it to search until it finds a particular phrase, here it would be 'red meat dishes', 'pork dishes', 'shellfish dishes' or 'other fish dishes'. Some of the individual words in these phrases may appear in the text that I want to extract, but the phrase itself will not appear in the text.
I don't really understand what you mean by
"Some of the individual words in these phrases may appear in the text that I want to extract".
Do you mean words like 'red', 'meat', 'dishes', etc, appears in the text individually? But their font are black, right?

"but the phrase itself will not appear in the text."
the phrase will not appear in the text? Where? In the regional.doc or in the new document (country. doc)?
In your sample document, they did appear in both document.

The thing with finding and manipulating document content is we have to choose to use some pattern. And sometimes the pattern could work in sample document but not in real document.
I can't promise you anything, but I will try to find a solution to your problem.

llyamah
03-21-2012, 02:46 AM
Hi akuini

Really sorry for the delayed reply - I checked my emails to see if I had a reply on this, but did not check the thread. Couldn't see anything in my emails, so apologies again.

The non-black font does indicate the beginning of a section - but note that 'shellfish dishes' is a subsection of the 'meat and fish' section and is in black bold font (non-underlined).

What I mean is this: In the text that I want to extract, you may see some of the words that make up the section name or sub-section name, but not the whole section name. For example, in my sample document you'll see that, under the 'Alcoholic Beverages' section, the word 'alcoholic' appears for the 'region' entry, but I'll never write 'alcholic beverages'. Hopefully this will help with the coding?

Thanks for all of your help.

akuini
03-21-2012, 05:23 PM
Hi, Ilyamah
I've written several sub procedures to create a specific country report, as you wanted.
So, the main task is to extract data relating to a specific country. This can be accomplished by those procedures.
The next task is related to some details, such as adding text "No information to report.", removing some of the hyperlinks, change the header, etc. Well, the sub procedures still needs some adjustments.

The main procedure is the "Sub createReport". Running this procedure will display the form "frmCountry". After selecting a country then this procedure will call some other procedure. The complete procedure and the form (.frm .frx .bas) are in the attachment.
BTW: in the Ireland report you erased the 'region' and 'country' section but the hyperlinks are still there, why?
See how it works.

Sub createReport()
Dim Rng As Range
Dim Rng1 As Range
Dim rngToExtract As Range
Dim m As Long

sCountryName = ""
bSelectCountry = True
frmCountry.Show
If bSelectCountry = False Then Exit Sub
Set Rng = ActiveDocument.Range
Rng.Copy
Documents.Add

Set Rng = Selection.Range
Rng.Paste

Rng.HighlightColorIndex = wdYellow

Set Rng = ActiveDocument.Range

With Rng.Find
.ClearFormatting
.Font.Bold = True
.Font.Underline = wdUnderlineSingle

Do While .Execute(FindText:=sCountryName & "^p", Forward:=True) = True

Set Rng1 = ActiveDocument.Range
Rng1.Start = Rng.End
m = Rng1.Start

With Rng1.Find
.ClearFormatting
.Font.Bold = True
.Font.Underline = True
.MatchWildcards = True
.Execute FindText:="[A-Z]", Format:=True, Forward:=True

If .Found = True Then
Set rngToExtract = Rng1.Duplicate
rngToExtract.Start = m
rngToExtract.End = Rng1.Start
rngToExtract.HighlightColorIndex = wdNoHighlight
End If

If Rng1.End = ActiveDocument.Range.End Then
Rng1.HighlightColorIndex = wdNoHighlight
End If

End With

Loop

End With
Call deleteSomePart
Call insertNoInfo
End Sub