Lefemmenikit
12-31-2020, 08:52 PM
Hi There
I have word documents of varying lengths which contain multiple invoices.
The invoices are 'separated' by continuous section breaks.
I am trying to:
a.) add the same header and footer to all pages within the document
b.) split the document into separate files for each of the invoices (i.e. split the document by the section breaks)
c.)related to b.), preserve the formatting, layout and appearance of the header and footer for the split files
For b.), I am using the macro found under: thread 223-Solved-Save-Word-Pages-or-Sections-as-Single-Individual-Documents on this site (I can't link this here since I am a new user)
Sub BreakIt()
Dim MainDoc As Document, SubDoc As Document, SectionNo%, sPath$
Set MainDoc = ActiveDocument
sPath = MainDoc.Path
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
For SectionNo = 1 To ActiveDocument.Sections.Count
ActiveDocument.Sections(SectionNo).Range.Copy
Set SubDoc = Application.Documents.Add
SubDoc.Range.Paste
SubDoc.SaveAs sPath & Left(MainDoc.Name, Len(MainDoc.Name) - 4) & _
SectionNo & ".doc"
SubDoc.Close
Next SectionNo
Set SubDoc = Nothing
Set MainDoc = Nothing
End Sub
At the moment, when I run this code, it splits the document into more files than there are visible sections
Looking at the document mark ups I can see this at all the section breaks:
27660
How do I fix this so that it is treated as one section break? (and therefore, splits the document into one file at this point)?
Related to this, some of the split files contain the headers and footers while others don't.
I am assuming this is related to the same issue of there being multiple section breaks (with not all of them being visible)?
27661
When I refer to 'visible' section breaks, I mean like those in the image above. I hope this makes sense
thanks
I have word documents of varying lengths which contain multiple invoices.
The invoices are 'separated' by continuous section breaks.
I am trying to:
a.) add the same header and footer to all pages within the document
b.) split the document into separate files for each of the invoices (i.e. split the document by the section breaks)
c.)related to b.), preserve the formatting, layout and appearance of the header and footer for the split files
For b.), I am using the macro found under: thread 223-Solved-Save-Word-Pages-or-Sections-as-Single-Individual-Documents on this site (I can't link this here since I am a new user)
Sub BreakIt()
Dim MainDoc As Document, SubDoc As Document, SectionNo%, sPath$
Set MainDoc = ActiveDocument
sPath = MainDoc.Path
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
For SectionNo = 1 To ActiveDocument.Sections.Count
ActiveDocument.Sections(SectionNo).Range.Copy
Set SubDoc = Application.Documents.Add
SubDoc.Range.Paste
SubDoc.SaveAs sPath & Left(MainDoc.Name, Len(MainDoc.Name) - 4) & _
SectionNo & ".doc"
SubDoc.Close
Next SectionNo
Set SubDoc = Nothing
Set MainDoc = Nothing
End Sub
At the moment, when I run this code, it splits the document into more files than there are visible sections
Looking at the document mark ups I can see this at all the section breaks:
27660
How do I fix this so that it is treated as one section break? (and therefore, splits the document into one file at this point)?
Related to this, some of the split files contain the headers and footers while others don't.
I am assuming this is related to the same issue of there being multiple section breaks (with not all of them being visible)?
27661
When I refer to 'visible' section breaks, I mean like those in the image above. I hope this makes sense
thanks