Create new .doc files in every heading level

J

Jim

Hi All,

I'm a newbie on VB and I need little help. I have a long .doc document
and my target is create new docs on every heading level.

For example my document looks like this:

1. General
This is general.
1.1 Cars
Here you can find cars.
1.2 Houses
Here you cand find houses.
2. Theory
This is theory.

So if I have a nice script it creates 4 files:
general.doc, cars.doc, houses.doc and theory.doc.

These files contains text between headings e.g. general.doc includes
text "This is general" and houses.doc "Here you can find houses"

I try to build a script but I found too many problems :(. Here is the
body of my code.


Sub create_files_from_headings()

all_Headings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)

For i = 1 To UBound(all_Headings)

MsgBox (myHeadings(i)) 'only for test purpose

ActiveDocument.Content.InsertAfter Text:=myHeadings(i)

Next i

End Sub

Thanks in advance !
 
J

Jean-Guy Marcil

Jim said:
Hi All,

I'm a newbie on VB and I need little help. I have a long .doc document
and my target is create new docs on every heading level.

For example my document looks like this:

1. General
This is general.
1.1 Cars
Here you can find cars.
1.2 Houses
Here you cand find houses.
2. Theory
This is theory.

So if I have a nice script it creates 4 files:
general.doc, cars.doc, houses.doc and theory.doc.

These files contains text between headings e.g. general.doc includes
text "This is general" and houses.doc "Here you can find houses"

I try to build a script but I found too many problems :(. Here is the
body of my code.


Sub create_files_from_headings()

all_Headings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)

For i = 1 To UBound(all_Headings)

MsgBox (myHeadings(i)) 'only for test purpose

ActiveDocument.Content.InsertAfter Text:=myHeadings(i)

Next i

End Sub

Here is somthing that is a bit twisted, but it works. I am sure someone will
come up with a more elegant solution that does not use so many flags! There
must be a way with the Find dialog, but I do not have time to look into it
right now...


Sub create_files_from_headings()

Dim docTarget As Document
Dim docSource As Document
Dim strPath As String
Dim i As Long
Dim rgeTarget As Range
Dim boolStart As Boolean
Dim boolCreate As Boolean
Dim boolEnd As Boolean

strPath = "C:\My Documents\Test\"
Set docSource = ActiveDocument
boolStart = False
boolCreate = False
boolEnd = False
i = 1

Application.ScreenUpdating = False

With docSource.Range.Paragraphs
For i = 1 To .Count
If i = .Count Then
rgeTarget.End = docSource.Range.End
boolStart = False
boolCreate = True
boolEnd = True
End If
If (.Item(i).Style = docSource.Styles(wdStyleHeading1) Or _
.Item(i).Style = docSource.Styles(wdStyleHeading2)) Or _
boolEnd Then
If Not boolEnd Then
If Not boolStart Then
Set rgeTarget = .Item(i).Range
boolStart = True
Else
rgeTarget.End = .Item(i).Range.Start
boolStart = False
boolCreate = True
i = i - 1
End If
End If
If boolCreate Then
Set docTarget = Application.Documents.Add
With docTarget
.Range.FormattedText = rgeTarget
.SaveAs strPath & .Range.Words(1).Text
.Close
boolCreate = False
End With
End If
End If
Next
End With

Application.ScreenUpdating = True
Application.ScreenRefresh

End Sub


Also, you will need to add something to check that each document created has
a unique name, otherwise the latesr ones will overwrite the previous ones. I
guess that you could add the "i" to the name to ensure a certain uniqueness.
You could change the line
.SaveAs strPath & .Range.Words(1).Text
for
.SaveAs strPath & i & "_" & .Range.Words(1).Text
 
F

frank112

Thank you very much for this peace of code even though I wasn't the one
asking the question. In relation to that however. Has the code been worked on
to make the process more efficient? I tried to rund the code i word 2007 on a
document of about 100 pages and it takes like 30 minutes. Therefore if
anybody know a faster or better way of coding this please help out.
Thanks in advance A+
frank112
 
J

Jean-Guy Marcil

frank112 said:
Thank you very much for this peace of code even though I wasn't the one
asking the question. In relation to that however. Has the code been worked on
to make the process more efficient? I tried to rund the code i word 2007 on a
document of about 100 pages and it takes like 30 minutes. Therefore if
anybody know a faster or better way of coding this please help out.

Every time you run through the paragraph collection, the code is slow.

You could use Range.Find to speed things up.

I do not have the time right now to rewrite the code and test it, but in a
nutshell: Define a range for the document.
Then find the first Heading 1 paragraph.
Then find the first Heading 2. paragraph.
Set a range ("Found1") starting at whichever range is the first in the
document (You would assume that it shold be HEading 1, but you never know!).
Then repeat the process, but now set a range starting at the end of the
found paragraph to the end of the document (It might be a heading 1 or a
Heading 2).
When you find the next heading, set the first range ("Found1") to finish
where the second found heading starts.
Export that range to a new document.
Rinse and repeat...
 
F

fumei via OfficeKB.com

Jean-Guy is right. Use Range. However, the BIG question is:

1. General
This is general.
1.1 Cars
Here you can find cars.
1.2 Houses
Here you cand find houses.
2. Theory
This is theory.

So if I have a nice script it creates 4 files:
general.doc, cars.doc, houses.doc and theory.doc.

OK. But does general.doc INCLUDE Cars.doc and Houses.doc.?

In other words:

Doc_A (General) = 1. General
This is general.
1.1 Cars
Here you can find cars.
1.2 Houses
Here you cand find houses.

Doc_B (Cars) =
1.1 Cars
Here you can find cars.

Doc_C (Houses) =
1.2 Houses
Here you cand find houses.

Doc_D (Theory) =
2. Theory
This is theory.

Or do you really want to separate it, like:

Doc_A (General) = 1. General
This is general.

Doc_B (Cars) =
1.1 Cars
Here you can find cars.

Doc_C (Houses) =
1.2 Houses
Here you cand find houses.

Doc_D (Theory) =
2. Theory
This is theory.

This is a logic specification, and must be settled. Both could be done using
the general suggestions Jean-Guy made...but you have to decide.
 
F

frank112

The ansver to your question is, no it only includes the content until it hits
the next heading with a style number 1 or 2. But that can be defined
dependently on the level you describe in the code.
 

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