Option Explicit
Sub GetList()
Dim MyDoc As Document
Dim Source As Document
Dim MyFile As String
Dim Location As String
Dim Extract As String
Dim txt As String
Dim txtBlock As Range
Dim x As Long
'Set number of paragraphs to be returned.
Const ListParas = 5
Set MyDoc = ActiveDocument
'Clear previous data
MyDoc.Range.Delete
'Browse for or set location
'Location = BrowseForFolder & "\"
Location = "C:\Source\Minor\"
'Set tabs etc. for imported data
With Selection.ParagraphFormat
.LeftIndent = CentimetersToPoints(2#)
.FirstLineIndent = CentimetersToPoints(-2#)
.TabStops.Add Position:=CentimetersToPoints(1#), _
Alignment:=wdAlignTabLeft
.TabStops.Add Position:=CentimetersToPoints(2#), _
Alignment:=wdAlignTabLeft
End With
'Set location
ChangeFileOpenDirectory Location
MyFile = Dir(Location & "*.DOC")
If MyFile = "" Then Exit Sub
With MyDoc
'Insert path as header
.Range.InsertAfter Location & vbCr & vbCr
.Paragraphs(1).Range.Bold = True
Do
If MyFile <> MyDoc.Name Then
'Open file and get text from selected number of paragraphs
Set Source = Documents.Open(FileName:=MyFile, Visible:=False)
Extract = Source.Range(Source.Paragraphs(1).Range.Start, _
Source.Paragraphs(ListParas).Range.End).Text
Source.Close
'Add filename as hyperlink at end of document
MyDoc.Hyperlinks.Add Anchor:=MyDoc.Bookmarks("\EndOfDoc").Range, _
Address:=Location & MyFile, TextToDisplay:=CStr(MyFile)
'Add Extract with breaks at end of document
txt = vbCr & Extract & vbCr
.Range.InsertAfter txt
'Format block to keep together
x = .Paragraphs.Count
Set txtBlock = .Range(.Paragraphs(x - (ListParas + 2)).Range.Start, _
.Paragraphs(x - 2).Range.End)
txtBlock.ParagraphFormat.KeepWithNext = True
End If
'Get next file
MyFile = Dir
Loop Until MyFile = ""
End With
End Sub
|