Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-14-2017, 05:24 PM
macropod's Avatar
macropod macropod is offline keep specific paragraphs in word documents in a folder Windows 7 64bit keep specific paragraphs in word documents in a folder 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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
keep specific paragraphs in word documents in a folder Saving new word documents to specific files maxbeedie Word 1 11-15-2016 04:04 AM
keep specific paragraphs in word documents in a folder Import a specific range (bookmarked section) from all Word docs in a selected folder Greengecko Word VBA 5 06-14-2016 07:54 AM
keep specific paragraphs in word documents in a folder VBA to set format for paragraphs that meet with specific requirements AustinBrister Word VBA 3 06-01-2015 07:00 AM
Office 2010 Can't Open Or Save Documents in My Documents Folder trippb Office 1 07-12-2013 07:29 AM
Print attachment when it arrive in specific folder with specific subject visha_1984 Outlook 1 01-30-2013 10:42 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:22 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft