Dear all,
I have been looking for a way to copy from a Word Document into a new one all headings up to level 3 (they use the style 'Heading 1', 'Heading 2', 'Heading 3'), all figures and all tables with their titles (they use the style 'caption') and their legends (they use the style 'legend'). All figures are inline shapes.
I came upon this thread
https://www.msofficeforums.com/word-...html#post18441 with Macropod as contributor and flds. I started focusing the code to my needs (see below). Although it is not a must for me to get the headings, I would like to get the other elements i.e., all figures and all tables with their titles (they use the style 'caption') and their legends (they use the style 'legend').
Thank you very much for your help!
Code:
Sub ExtractTabFigDoc()
Application.ScreenUpdating = False
Dim strInFold As String, strOutFold As String, strFile As String, strOutFile As String
Dim TOC As TableOfContents, Para As Paragraph, tbl As Table, Sctn As Section, Rng As Range
Dim DocSrc As Document, DocTbl As Document
'Call the GetFolder Function to determine the folder to process
strInFold = GetFolder
If strInFold = "" Then Exit Sub
strFile = Dir(strInFold & "\*.doc", vbNormal)
'Check for documents in the folder - exit if none found
If strFile <> "" Then strOutFold = strInFold & "\Output"
'Test for an existing outpfolder & create one if it doesn't already exist
If Dir(strOutFold, vbDirectory) = "" Then MkDir strOutFold
strFile = Dir(strInFold & "\*.doc", vbNormal)
'Process all documents in the chosen folder
While strFile <> ""
Set DocSrc = Documents.Open(FileName:=strInFold & "\" & strFile, AddTorecentFiles:=False, Visible:=False)
With DocSrc
'Delete everything before the first Table Of Contents in the source document
If .TablesOfContents.Count <> 0 Then
Set Rng = .TablesOfContents(1).Range
Rng.Start = .Range.Start
Rng.Delete
End If
'Delete any other Tables Of Contents in the source document
For Each TOC In .TablesOfContents
TOC.Delete
Next
'Check for tables in the source document
If .Tables.Count > 0 Then
'If there are any tables in the source document, make a copy of the document
.Range.Copy
' Create a new document for the tables
Set DocTbl = Documents.Add(Visible:=False)
'Process the new document
Call MakeTableFigDoc(DocTbl)
'Delete all tables in the source document
For Each tbl In .Tables
tbl.Delete
Next
End If
Call Cleanup(.Range)
'String variable for the output filenames
strOutFile = strOutFold & "\" & Split(.Name, ".")(0)
'Save and close the tables document
If Not DocTbl Is Nothing Then
DocTbl.SaveAs FileName:=strOutFile & "-TablesFigures", AddTorecentFiles:=False
DocTbl.Close
Set DocTbl = Nothing
End If
'Close the source document without saving the changes we've made to it
.Close SaveChanges:=False
End With
strFile = Dir()
Wend
Set Rng = Nothing: Set DocSrc = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder(Optional Title As String, Optional RootFolder As Variant) As String
On Error Resume Next
GetFolder = CreateObject("Shell.Application").BrowseForFolder(0, Title, 0, RootFolder).Items.Item.Path
End Function
Sub MakeTableFigDoc(DocTbl As Document)
Dim Sctn As Section, Para As Paragraph, Rng As Range
With DocTbl
.Range.Paste
'Delete any Sections with no tables in the tables document
For Each Sctn In .Sections
If Sctn.Range.Tables.Count = 0 Then
Sctn.Range.Delete
Else
On Error Resume Next
If Sctn.PageSetup.Orientation = Sctn.Range.Previous.PageSetup.Orientation Then
Sctn.Range.Previous.Characters.Last.Delete
End If
End If
Next
'Check all paragraphs not in tables in the tables document
For Each Para In .Paragraphs
With Para
Set Rng = .Range
On Error Resume Next
With Rng
If .Information(wdWithInTable) = False Then
If .Next.Paragraphs.First.Range.Information(wdWithInTable) = False Then
'Delete any paragraphs not followed by a table in the tables document
.Delete
Else
'Keep table captions, if present
If InStr(.Style, "Caption") = 0 Then
.End = .End - 1
.text = vbNullString
End If
.InsertBefore vbCr
End If
End If
End With
End With
Next
End With
Set Rng = Nothing
End Sub
Sub NewDoc(NewDoc As Document)
With NewDoc
.Range.Paste
Call Cleanup(.Range)
End With
End Sub
Sub Cleanup(Rng As Range)
With Rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.text = "^b"
.Replacement.text = ""
.Execute Replace:=wdReplaceAll
End With
End Sub