Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-14-2017, 10:05 AM
modiria50989 modiria50989 is offline keep specific paragraphs in word documents in a folder Windows 8 keep specific paragraphs in word documents in a folder Office 2010 64bit
Banned
keep specific paragraphs in word documents in a folder
 
Join Date: Aug 2017
Posts: 7
modiria50989 is on a distinguished road
Default keep specific paragraphs in word documents in a folder

Hi, I have a folder of 41 word documents in it. I need to open each of them, then copy and paste specific paragraphs (which I know their start and end keywords) into a new page. At the end I have one page with all the information I want from all the 41 files.



I'm not sure but I think it's better if i mix all 41 files into 1 file, then I can try to figure out how I can keep my specific paragraphs and delete the rest.

Please give me a VBA code to do that. Thanks.
Reply With Quote
  #2  
Old 08-14-2017, 04:34 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: 21,962
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

It is by no means clear from your post whether you're looking for:
• a single string with your known start and end keywords;
• multiple strings with the same start and end keywords; or
• singular/multiple strings with different start and end keywords,
in the documents. The code for each is quite different and, with the latter two, the question of where Word would look for the keywords comes into the equation.

Some clarification would be useful.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 08-14-2017, 04:57 PM
modiria50989 modiria50989 is offline keep specific paragraphs in word documents in a folder Windows 8 keep specific paragraphs in word documents in a folder Office 2010 64bit
Banned
keep specific paragraphs in word documents in a folder
 
Join Date: Aug 2017
Posts: 7
modiria50989 is on a distinguished road
Default

Thank you for your comment and I'm sorry if it's not clear enough. Assuming in a folder, for each word document, we have bunch of lines, tables, and paragraphs. But there are 4 identical lines which are the same in all word documents. They are:" Hello world", "goodby my life", ”im an electrical engineering major", "we are the best"

Now I want to keep everything between "Hello world" and "goodby my life"
Also i want to keep everything between "im an electrical engineering major " and "we are the best"
I don't want to see the rest of lines and data in the page.
Reply With Quote
  #4  
Old 08-14-2017, 04:59 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: 21,962
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

Do you need to be able to define different start and end keywords, or can they be hard-coded?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 08-14-2017, 05:05 PM
modiria50989 modiria50989 is offline keep specific paragraphs in word documents in a folder Windows 8 keep specific paragraphs in word documents in a folder Office 2010 64bit
Banned
keep specific paragraphs in word documents in a folder
 
Join Date: Aug 2017
Posts: 7
modiria50989 is on a distinguished road
Default

I'm fine with whatever you're fine with, because if my keword lines change, i can replace the string with the old one iside the code. However there is no more than 4 keyword lines.
Reply With Quote
  #6  
Old 08-14-2017, 05:07 PM
modiria50989 modiria50989 is offline keep specific paragraphs in word documents in a folder Windows 8 keep specific paragraphs in word documents in a folder Office 2010 64bit
Banned
keep specific paragraphs in word documents in a folder
 
Join Date: Aug 2017
Posts: 7
modiria50989 is on a distinguished road
Default

Sorry, one more thing. Just make sure all 4 lines are included in the resuls
Reply With Quote
  #7  
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: 21,962
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
  #8  
Old 08-14-2017, 05:31 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: 21,962
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

Cross-posted at: http://www.vbaexpress.com/forum/show...elete-the-rest
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


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 12:27 AM.


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