1. it would be easier to copy the stuff you want into another document
rather than into the current one. As a separate exercise, you might append
the second document to the first; but do that later. It's much easier to
process a document if it's not changing.
2. 'Lines of text with carriage returns' are called 'paragraphs' in Word.
3. The basic method is to iterate the paragraphs, copying those you need --
Dim pPar as Word.Paragraph
For each pPar in Doc1.Paragraphs
if instr(pPar.Text, "START REPORT") > 0 then
...
etc
Next
>I have a Word document that contains hundreds of similarly-formatted
> reports (about 30-40 lines of text with carriage returns), with lines
[quoted text clipped - 8 lines]
>
> Any thoughts would be greatly appreciated. Thanks!
Hi trgobeille
One way to achieve your aim here is to re-think the process. Instead of
copying the text from the body to the bottom of the document, it would be
much easier to mark in the text what's required in the list at the bottom of
the document, and then generate that list.
To do that, you can use Word's table of contents functionality. Get Word to
create a table of contents that consists only of the paragraphs marked with
a certain style. And, tell Word to omit the page numbers from the table of
contents.
So the first thing to consider is what styles have been used in the
document. If the document is all in one style, then you could create a new
style and call it something like "Summary". Base it on Normal or whatever
style is used in the document for ordinary text. That is, it can *look* just
like the surrounding text.
For the "LAUNCH DATE" and "DETAILS" paragraphs, you can use Edit > Replace
to search for your text and replace it with the same text, but with your new
style. (Click in the "Replace with" box, type the replacement text, which is
the same as the find text, click More and select your style.)
Now, at the bottom of the document, type the following (be careful of where
the spaces go):
TOC \t "Summary,1" \n"1-1"
Select all that, and do (in the following order): ctrl-F9, F9, Shift-F9.
That will give you a 'table of contents' of all the text you marked with
your Summary style. At the very end of the process, if you need the list to
be plain text, you can convert the
field. Put your cursor in the 'table of contents' and do ctrl-Shift-F9.
So far, it's taken longer to explain this than it will to do it!
However, that won't cope with the need to get the paragraph *after* the
'START REPORT' text. Is there some other way you can easily identify the
paragraphs you need in your list, so you could search for some specific text
and apply your Summary style to that? If not, then you'll need a macro.
Here's a simple one that goes through the document, and applies the Summary
style to every paragraph *after* a paragraph containing "START REPORT"
Sub MarkParaAfterStartReportAsSummary()
Dim wdStyle As Word.Style
Dim oRange As Word.Range
'Make sure that there's a style called "Summary"
On Error Resume Next
Set wdStyle = ActiveDocument.Styles("Summary")
On Error GoTo 0
If wdStyle Is Nothing Then
Set wdStyle = ActiveDocument _
.Styles.Add("Summary")
wdStyle.BaseStyle = wdStyleNormal
End If
'Display field codes, not results
ActiveWindow.View.ShowFieldCodes = True
'Find all text that says "START REPORT"
Set oRange = ActiveDocument.Content
With oRange
.Find.ClearFormatting
With .Find
.Text = "START REPORT"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While .Find.Execute
'If this is not in a field,
'apply the Summary style to the
'*next* paragraph
If Not oRange.Paragraphs(1).Range.Fields.Count > 0 Then
oRange.Move wdParagraph, 1
oRange.Style = "Summary"
End If
Loop
End With
'Display field results
ActiveWindow.View.ShowFieldCodes = False
End Sub
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
>I have a Word document that contains hundreds of similarly-formatted
> reports (about 30-40 lines of text with carriage returns), with lines
[quoted text clipped - 8 lines]
>
> Any thoughts would be greatly appreciated. Thanks!
trgobeille@gmail.com - 07 Aug 2006 03:44 GMT
The formatting solution makes me nervous, because the source documents
are concatenated from about 1200 different files (various types) and so
the formatting has lots of fun variations lurking around within the
document.
Please forgive my bad coding, but here's my first cut. It does the
extraction for the first report just fine, but I'm not sure how to code
the looping back to the start of the document. I'm thinking that it
would be better to shoot all of the parsed stuff into a new document,
but I'm already pushing the boundaries of my capability.
Sub ExtractionTest()
Dim docName As String
Dim fltDetail As String
Dim timeSpec As String
With Selection.Find
.Text = "START REPORT"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If Selection.Find.Execute Then
Selection.MoveDown Unit:=wdParagraph, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
docName = Selection
Selection.MoveRight Unit:=wdWord, Count:=1
With Selection.Find
.Text = "FLTD"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
If Selection.Find.Execute Then
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
fltDetail = Selection
Selection.MoveRight Unit:=wdWord, Count:=1
With Selection.Find
.Text = "TIMES"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
If Selection.Find.Execute Then
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
timeSpec = Selection
Selection.EndKey Unit:=wdStory
Selection.TypeText (docName)
Selection.TypeText (fltDetail)
Selection.TypeText (timeSpec)
End If
End If
End If
End Sub
Jezebel - 07 Aug 2006 05:10 GMT
That really is the hard way to do it. Iterating the paragraphs is much
simpler. But if you must use Find, work with the document range rather than
the Selection.
> The formatting solution makes me nervous, because the source documents
> are concatenated from about 1200 different files (various types) and so
[quoted text clipped - 86 lines]
>
> End Sub
Gobi - 07 Aug 2006 13:00 GMT
> That really is the hard way to do it. Iterating the paragraphs is much
> simpler. But if you must use Find, work with the document range rather than
> the Selection.
I'll check into that. Thanks!