#1
|
|||
|
|||
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. |
#2
|
||||
|
||||
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] |
#3
|
|||
|
|||
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. |
#4
|
||||
|
||||
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] |
#5
|
|||
|
|||
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.
|
#6
|
|||
|
|||
Sorry, one more thing. Just make sure all 4 lines are included in the resuls
|
#7
|
||||
|
||||
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] |
#8
|
||||
|
||||
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] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Saving new word documents to specific files | maxbeedie | Word | 1 | 11-15-2016 04:04 AM |
Import a specific range (bookmarked section) from all Word docs in a selected folder | Greengecko | Word VBA | 5 | 06-14-2016 07:54 AM |
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 |