Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-29-2016, 09:19 PM
macropod's Avatar
macropod macropod is offline Extract Data from Word Documents Windows 7 64bit Extract Data from Word Documents Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default


Try the following macro:
Code:
Sub CollateDocumentHeadings()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, i As Long, Rng As Range
Dim wdDocSrc As Document, wdDocTmp As Document, wdDocOut As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set wdDocTmp = Documents.Add
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  i = 0
  If ThisDocument.FullName <> strFolder & "\" & strFile Then
    Set wdDocSrc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDocSrc
      With .Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = ""
          .Replacement.Text = ""
          .Format = True
          .Style = wdStyleHeading1
          .Execute
        End With
        Do While .Find.Found
          i = i + 1
          Set Rng = .Duplicate
          With Rng
            .End = .End - 1
          End With
          With wdDocTmp
            If .Sections.Count < i Then
              .Range.InsertAfter vbCr
              .Sections.Add Range:=.Characters.Last, Start:=wdSectionNewPage
            End If
            .Sections(i).Range.Characters.Last.InsertBefore Rng.Text & vbTab & strFile & vbCr
          End With
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      .Close SaveChanges:=False
    End With
  End If
  strFile = Dir()
Wend
With wdDocTmp
  .SaveAs2 FileName:=strFolder & "\Headings Master List.docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
  For i = 1 To .Sections.Count
    Set Rng = .Sections(i).Range
    With Rng
      .MoveEnd wdCharacter, -1
    End With
    Set wdDocOut = Documents.Add(Visible:=False)
    With wdDocOut
       .Range.FormattedText = Rng.FormattedText
      .SaveAs FileName:=strFolder & "\Headings (" & i & ") List.docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
      .Close SaveChanges:=True
    End With
  Next
End With
Set wdDocSrc = Nothing: Set wdDocTmp = Nothing: Set wdDocOut = 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]
Reply With Quote
Reply

Tags
data, extract, word 2013



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract the filter data Sandhya Excel Programming 2 10-25-2015 09:02 AM
Extract Data from Word Documents Extract Excel Data from Chart in Word cillianmccolgan Word 1 08-15-2014 01:42 AM
Extract Data from Word Documents How to extract data from Excel database to create word report for each patient (row) nightale Word 3 07-06-2014 04:17 PM
Extract Data from Word Documents How to extract two different output documents from 1 master document? wimvanrompuy Word 3 06-20-2014 06:43 AM
Extract Data from Word Documents How to Extract key data from word iliauk Word 3 11-08-2013 04:37 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft