Word VBA >> Creating Multiple Documents from a Single Documents

by tmb4f » Sat, 24 Jul 2004 02:30:19 GMT

I have an .RTF document containing a TOC and roughly 200 pages or
reports. The TOC contains the 200 line-items (i.e. Heading 1) linking
to the 200 reports. I have been asked to separate this one document
into 200 separate documents. Borrowing from a macro I found searching
this group (Sub BreakOnHeading), I have been able to complete 95% of
this task. The only thing I have left to do is to somehow give each
separate document a unique file name. Ideally, I would like this file
name to incorporate some of the text from the TOC line-items. The
current code, seen below, just adds a number to the name of the master
document to generate a subdocument name:

.SaveAs FileName:=sCurrentPath & "\" & sCurrentDocStripped & i

Note: The .RTF document was generated using SAS. As such, each
report consists of 3 tables: a Titles/Heading table, a Data table, and
a Footnotes table. The last row in the Titles/Heading table contains
the Heading 1-style text referenced in the TOC.

Any ideas you may have about generating more descriptive file names
would be greatly appreciated.

Thank you.


Sub ParseClinicActivityReport()

Dim sCurrentDoc As String
Dim sCurrentDocStripped As String
Dim sCurrentPath As String
Dim i As Integer
Dim iNumBookmarks As Integer
Dim rng1 As Range
Dim rng2 As Range
Dim prng1 As Range
Dim prng2 As Range

sCurrentDoc = ActiveDocument.Name
sCurrentDocStripped = Left(sCurrentDoc, Len(sCurrentDoc) - 4)
sCurrentPath = ActiveDocument.Path

ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation
iNumBookmarks = ActiveDocument.Bookmarks.Count

For i = 1 To iNumBookmarks

Set rng1 = ActiveDocument.Range.Bookmarks.Item(i).Range


Set prng1 = ActiveDocument.Bookmarks("\Page").Range

If i + 1 <= iNumBookmarks Then
Set rng2 = ActiveDocument.Range.Bookmarks.Item(i + 1).Range


Set prng2 = ActiveDocument.Bookmarks("\Page").Range

prng1.Start = prng1.Start + 1
prng1.End = prng2.Start - 1
Else: prng1.End = ActiveDocument.Range.End
End If

If prng1.Characters.Count > 1 Then


With ActiveDocument
.PageSetup.TopMargin = InchesToPoints(0.25)
.PageSetup.LeftMargin = InchesToPoints(0.25)
.PageSetup.BottomMargin = InchesToPoints(0.25)
.PageSetup.RightMargin = InchesToPoints(0.25)
.PageSetup.Gutter = InchesToPoints(0)
.PageSetup.GutterPos = wdGutterPosLeft
.PageSetup.Orientation = wdOrientLandscape
.PageSetup.HeaderDistance = InchesToPoints(0.5)
.PageSetup.FooterDistance = InchesToPoints(0.5)
.SaveAs FileName:=sCurrentPath & "\" &
sCurrentDocStripped & i
End With
End If

Next i
Selection.Collapse wdCollapseEnd
MsgBox "Finished!", vbOKOnly

End Sub

Word VBA >> Creating Multiple Documents from a Single Documents

by tmb4f » Wed, 28 Jul 2004 00:12:24 GMT

My "Note:" lead me to the answer. Before doing the .SaveAs, I went to
the table containing the information I wanted to include in the
filename (Set myTable = ActiveDocument.Tables(1)) and pulled the text
out (sCurrentDocName = myTable.Cell(4,1).Range.Text)

Similar Threads

1. Creating Multiple Documents from a Single Document - Office Word Programming

2. How to create single TOC from multiple documents

3. Printing multiple instances of a single document on a single page

4. Copy multiple paragraphs from multiple documents into one document

I need to copy addresses from 1000 documents and put them into one document.  
I need a macro that will copy from all documents under one Folder and Paste 
them into another document, preferably a Table.  I can work with the 
formatting later.  I located a "How to Find & ReplaceAll on a batch of 
documents in the same folder",  but I am not good enough to modify it to 
Select the correct area and Copy and Paste instead.   

5. How do i join multiple documents into 1 single document - Word Document management

6. Inserting Single word document into multiple documents


Im looking to add a two page word document at the end of around 3000
CVs .Is there a way of doing this all at once? I have looked all over
the internet and all i can find is programs that will insert text into
open documents.I need to do it to a whole folder. Help is much



7. looking for sample:multiple view single document in C# or VB - .Net Framework

8. Multiple Page 1's in single document

I have a document that consists of multiple (say, 8) sub-documents.  For
ease of use, the creators want to create it in a single document.  The
trouble is each sub-document needs to be begin with Page 1.  How can I do