View Single Post
 
Old 08-14-2017, 05:24 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try running the following macro from an empty Word document. Simply edit the entries on the ArrStart & ArrEnd lines to refer to your actual start & end keywords. You can add/delete start & end keywords as necessary. Note that the keyword tests are case-sensitive.
Code:
Sub GetKeyWordStrings()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
Dim ArrStart(), ArrEnd(), i As Long, StrOut As String
ArrStart = Array("StartString1", "StartString2", "StartString3")
ArrEnd = Array("EndString1", "EndString2", "EndString3")
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    StrOut = StrOut & vbCr & strFile & vbCr
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      For i = 0 To UBound(ArrStart)
        With .Range
          With .Find
            .ClearFormatting
            .Format = False
            .Forward = True
            .Wrap = wdFindStop
            .MatchWildcards = True
            .Text = ArrStart(i) & "*" & ArrEnd(i)
            .Execute
          End With
          Do While .Find.Found
            StrOut = StrOut & .Text & vbCr
            .Collapse wdCollapseEnd
            .Find.Execute
          Loop
        End With
      Next
      .Close SaveChanges:=False
    End With
  End If
  strFile = Dir()
Wend
With ActiveDocument.Range
  .Text = StrOut
  .Characters.First.Delete
End With
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote