Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-19-2023, 02:26 PM
Winrow Winrow is offline Word Extract Caption Figure legend and Caption Table Legend to new Word document Windows 10 Word Extract Caption Figure legend and Caption Table Legend to new Word document Office 2016
Novice
Word Extract Caption Figure legend and Caption Table Legend to new Word document
 
Join Date: Jun 2022
Posts: 5
Winrow is on a distinguished road
Arrow Word Extract Caption Figure legend and Caption Table Legend to new Word document

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
Reply With Quote
Reply

Tags
copy, vba, word 16



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Extract Caption Figure legend and Caption Table Legend to new Word document Replace table and figure caption style to another style Word VBA laith93 Word VBA 7 08-27-2021 11:37 PM
Word Extract Caption Figure legend and Caption Table Legend to new Word document Trying to customize caption text for figure table canadianjameson Word 6 04-02-2018 07:44 PM
Data Key Legend Table in Excel Alaska1 Excel 1 05-19-2014 08:13 AM
Caption Order: Figure 4 Figure 3 Figure 2 golfarchitect13 Word 5 05-07-2014 07:15 PM
MS Word figure caption numbering skaboy607 Word 3 03-21-2012 02:31 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:46 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