Hi flds,
Try this new version of the 'ParseDocs' sub with other subs in my previous post:
Code:
Sub ParseDocs()
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, DocOutline As Document, DocTxt As Document, DocTbl As Document
Dim DocApp As Document, DocRef As Document, oShp As Shape, iShp As InlineShape
'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
Set DocOutline = Documents.Add(Visible:=False)
Call CreateOutline(DocSrc, DocOutline)
'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 TOC
'Convert all fields in the source document to plain text
.Fields.Unlink
With Content.Find
.ClearFormatting
.Replacement.ClearFormatting
'Convert non-breaking hyphens to ordinary hyphens
.Text = "^~"
.Replacement.Text = "-"
.Execute Replace:=wdReplaceAll
'Delete manual page breaks
.Text = "^m"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End
For Each oShp In .Shapes
oShp.Delete
Next oShp
For Each iShp In .InlineShapes
With iShp.Range.Paragraphs.First.Range
With .Next.Paragraphs.First
If .Style = "Caption" Then .Range.Delete
End With
.Delete
End With
Next iShp
'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 MakeTableDoc(DocTbl)
End If
'Delete all tables in the source document
For Each Tbl In .Tables
Tbl.Delete
Next Tbl
'Check for appendices in the source document
For Each Sctn In .Sections
If UCase(Trim(Sctn.Range.Words.First)) = "APPENDIX" Then
Set Rng = Sctn.Range
Rng.End = .Range.End
'Cut the from the start of the first appendices Section to the end of the
'source document and paste it into a new appendices document
Rng.Cut
Set DocApp = Documents.Add(Visible:=False)
'Process the new document
Call NewDoc(DocApp)
Exit For
End If
Next Sctn
'Check for References in the source document
For Each Sctn In .Sections
If UCase(Trim(Sctn.Range.Words.First)) = "REFERENCES" Then
Set Rng = Sctn.Range
Rng.End = .Range.End
'Cut the from the start of the first References Section to the end of the
'source document and paste it into a new references document
Rng.Cut
Set DocRef = Documents.Add(Visible:=False)
'Process the new document
Call NewDoc(DocRef)
Rng.End = .Range.End
Rng.Cut
Exit For
End If
Next Sctn
'Check for Design Requirements in the source document
For Each Sctn In .Sections
If UCase(Sctn.Range.Sentences.First) Like "#*DESIGN REQUIREMENT*" Then
Set Rng = Sctn.Range
'Cut the 'Design Requirement' Section from the
'source document and paste it into a new references document
Rng.Cut
Set DocDesReq = Documents.Add(Visible:=False)
'Process the new document
Call NewDoc(DocDesReq)
'Delete everything after the 'Design Requirement' Section
Rng.End = .Range.End
Rng.Delete
Exit For
End If
Next Sctn
Call Cleanup(.Range)
'String variable for the output filenames
strOutFile = strOutFold & Split(.Name, ".")(0)
'Copy whatever's left in the source document and paste it into a new text document
.Range.Copy
Set DocTxt = Documents.Add(Visible:=False)
With DocTxt
.Range.Paste
'Save and close the text document
.SaveAs FileName:=strOutFile & "-Text", AddTorecentFiles:=False
.Close
End With
Set DocTxt = Nothing
'Save and close the Outline document
With DocOutline
.SaveAs FileName:=strOutFile & "-Outline", AddTorecentFiles:=False
.Close
End With
'Save and close the tables document
If Not DocTbl Is Nothing Then
DocTbl.SaveAs FileName:=strOutFile & "-Tables", AddTorecentFiles:=False
DocTbl.Close
Set DocTbl = Nothing
End If
'Save and close the appendices document
If Not DocApp Is Nothing Then
DocApp.SaveAs FileName:=strOutFile & "-Appendices", AddTorecentFiles:=False
DocApp.Close
Set DocApp = Nothing
End If
'Save and close the references document
If Not DocRef Is Nothing Then
DocRef.SaveAs FileName:=strOutFile & "-References", AddTorecentFiles:=False
DocRef.Close
Set DocRef = Nothing
End If
'Save and close the design requirements document
If Not DocDesReq Is Nothing Then
DocDesReq.SaveAs FileName:=strOutFile & "-Design Requirements", AddTorecentFiles:=False
DocDesReq.Close
Set DocDesReq = 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 DocOutline = Nothing: Set DocSrc = Nothing
Application.ScreenUpdating = True
End Sub