Split document by Heading1

B

Brian

Hello all,

I have been trying to amend Dougs splitter macro to split the documents by
heading1 with little success. Any suggestions would be gratefully received.

Dim Counter As Long, Source As Document, Target As Document
Dim myRange As Range

Set myRange = ActiveDocument.Range.Style = wdStyleHeading1

Set Source = ActiveDocument

Selection.HomeKey Unit:=wdStory

'Section = Source.BuiltInDocumentProperties(wdSectionOddPage)

Counter = 0

While Counter < myRange

Counter = Counter + 1

DocName = "Heading1" & Format(Counter)

Source.Bookmarks("\myRange").Range.Cut

Set Target = Documents.Add

Target.Range.Paste

Target.SaveAs FileName:=DocName

Target.Close

Wend

Thanks,
 
J

Jean-Guy Marcil

Brian said:
Hello all,

I have been trying to amend Dougs splitter macro to split the documents by
heading1 with little success. Any suggestions would be gratefully received.

Here is a different approach that seems to work on Word 2003 with simple
documents:

Dim Counter As Long
Dim Source As Document
Dim Target As Document
Dim myRange As Range
Dim rgeFoundStart As Long
Dim Docname As String
Dim boolBegin As Boolean

Set Source = ActiveDocument
Set myRange = Source.Range
Counter = 0
boolBegin = False

With myRange.Find
.Style = Source.Styles(wdStyleHeading1)
Do While .Execute
If Not boolBegin Then
rgeFoundStart = myRange.Start
myRange.SetRange myRange.End, Source.Range.End
boolBegin = True
Else
boolBegin = False
myRange.SetRange rgeFoundStart, myRange.Start - 1
Set Target = Documents.Add
Counter = Counter + 1
Docname = "Heading1_" & Format(Counter)
With Target
.Range.FormattedText = myRange.FormattedText
.SaveAs FileName:=Docname
.Close
End With
myRange.SetRange myRange.End, Source.Range.End
End If
Loop
End With

'In case there was only one Heading 1 in the document
If boolBegin Then
myRange.SetRange rgeFoundStart, Source.Range.End
Set Target = Documents.Add
Counter = Counter + 1
Docname = "Heading1_" & Format(Counter)
With Target
.Range.FormattedText = myRange.FormattedText
.SaveAs FileName:=Docname
.Close
End With
End If
 
C

Cindy M.

Hi Brian,
I have been trying to amend Dougs splitter macro to split the documents by
heading1 with little success. Any suggestions would be gratefully received.
Personally, I usually use the Master Document feature to do this. You'll find
some sample code here

http://homepage.swissonline.ch/cindymeister/mergfaq2.htm#SepFile

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
B

Brian

Jean-Guy

Thanks for the code, but it didn't work. I think it is due to the section
breaks used throughout the document. The heading 1 comes after an odd page
section break. It is repeated in the headers. I used one outside the headers
for TOC purposes.

Thanks,
 
B

Brian

Cindy,

Thanks for the response. The companies firwall won't let me get to the link,
so I will try from home tonight.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top