![]() |
|
|
|
#1
|
||||
|
||||
|
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 |
|
#2
|
|||
|
|||
|
Hello Graham,
Thank you for helping out. I still have over 1000 word documents (.docx) to scan. Adding the section break in each of these documents will take forever. Is there a way to search between "System Safety Assessment Summary" and "Hardware Considerations" without adding the section break? Also, is there a way to output the filename along with the copied information? |
|
|
|
Similar Threads
|
||||
| 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 |
Applied Styles to Headings in Multi-Level List; now ALL second level headings are 1.XX
|
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 |