![]() |
#5
|
||||
|
||||
![]()
Your document has lots of manual page breaks. You could convert those to section breaks and then extract the appropriate sections to another document e.g. as follows, which will work with you sample.
Start with no documents open then run the following macro. I would recommend working with a small selection of documents in the selected folder to test the process works for you. Code:
Option Explicit Sub ExtractData() 'Graham Mayor - https://www.gmayor.com - Last updated - 23 Nov 2021 Dim oDoc As Document Dim oSource As Document Dim oRng As Range, oDocRng As Range Dim lSec As Long, lPara As Long Dim fDialog As FileDialog Dim strPath As String, strFile As String If Documents.Count = 0 Then Documents.Add Set oDoc = ActiveDocument Set fDialog = Application.FileDialog(msoFileDialogFolderPicker) With fDialog .Title = "Select folder and click OK" .AllowMultiSelect = False .InitialView = msoFileDialogViewList If .Show <> -1 Then MsgBox "Cancelled By User", , "List Folder Contents" Exit Sub End If strPath = fDialog.SelectedItems.Item(1) If Right(strPath, 1) <> "\" Then strPath = strPath + "\" End With strFile = Dir$(strPath & "*.docx") While strFile <> "" Set oSource = Documents.Open(strPath & strFile) Set oRng = oSource.Range With oRng.Find Do While .Execute("^m") oRng.Text = "" oRng.InsertBreak wdSectionBreakNextPage oRng.Collapse 0 Loop End With If Len(oDoc.Range) > 1 Then Set oRng = oDoc.Range oRng.Collapse 0 oRng.InsertBreak wdPageBreak End If For lSec = 1 To oSource.Sections.Count For lPara = 1 To oSource.Sections(lSec).Range.Paragraphs.Count If oSource.Sections(lSec).Range.Paragraphs(lPara).Range.Text Like "*System Safety Assessment Summary*" Then If oSource.Sections(lSec).Range.Paragraphs(lPara).Range.Style = "ForCertPlanTOC" Then Set oDocRng = oDoc.Range oDocRng.Collapse 0 oDocRng.FormattedText = oSource.Sections(lSec).Range.FormattedText oDocRng.InsertParagraphAfter Exit For End If End If Next lPara For lPara = 1 To oSource.Sections(lSec).Range.Paragraphs.Count If oSource.Sections(lSec).Range.Paragraphs(lPara).Range.Text Like "*Hardware Considerations*" Then If oSource.Sections(lSec).Range.Paragraphs(lPara).Range.Style = "ForCertPlanTOC" Then Set oDocRng = oDoc.Range oDocRng.Collapse 0 oDocRng.FormattedText = oSource.Sections(lSec).Range.FormattedText oDocRng.InsertParagraphAfter Exit For End If End If Next lPara Next lSec If oDoc.Sections.Count > 2 Then Set oRng = oDoc.Sections.Last.Range.Previous oRng.End = oDoc.Range.End oRng.Delete End If oSource.Close SaveChanges:=wdDoNotSaveChanges strFile = Dir$() Wend lbl_Exit: Set fDialog = Nothing Set oSource = Nothing Set oDoc = Nothing Set oRng = Nothing Set oDocRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Need to copy texts from excel and paste in to the Notepad++ in between the particular tags | ganesang | Word VBA | 2 | 08-27-2018 02:05 AM |
![]() |
NNL | Word | 1 | 08-09-2017 02:52 PM |
Numbered headings not working as expected after customising headings | seanspotatobusiness | Word | 5 | 03-03-2017 04:44 AM |
Copy, Paste, and Format Multiple Headings | Dretherix | Word VBA | 2 | 02-12-2016 08:26 AM |
Trying to find and copy all headings at the same time | WaltR | Word | 7 | 08-21-2012 03:12 PM |