Consulting

Results 1 to 5 of 5

Thread: Adding headers and footers to all sections of word doc and deleting blank sections

  1. #1

    Adding headers and footers to all sections of word doc and deleting blank sections

    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:
    rKa6YFb.png


    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)?


    When I refer to 'visible' section breaks, I mean like those in the image above. I hope this makes sense

    thanks

  2. #2
    Based on your comments, the following should work

    Sub BreakIt()
    'Graham Mayor - https://www.gmayor.com - Last updated - 01 Jan 2021 
    Dim MainDoc As Document, SubDoc As Document, SectionNo%, sPath$
    Dim oSection As Section
        Set MainDoc = ActiveDocument
        'remove the empty sections
        For Each oSection In MainDoc.Sections
            If Len(oSection.Range) < 3 Then oSection.Range.Delete
        Next oSection
        '==========
        sPath = MainDoc.path
        If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    
    
        For SectionNo = 1 To MainDoc.Sections.Count
            MainDoc.Sections(SectionNo).Range.Copy
            'base the new documents on the original document thus preserving the header/footer
            Set SubDoc = Application.Documents.Add(MainDoc.FullName)
            SubDoc.Range.Paste
            SubDoc.SaveAs sPath & Left(MainDoc.Name, Len(MainDoc.Name) - 4) & _
                          SectionNo & ".doc"  'doc format?
            SubDoc.Close
        Next SectionNo
        Set SubDoc = Nothing
        Set MainDoc = Nothing
        Set oSection = Nothing
    End Sub
    If all the documents are similarly formatted e.g. as a result of a mail merge, then see  https://www.gmayor.com/MergeAndSplit.htm
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thank you Gmayor.

    I tried the macro right now.

    the headers and footers appear on the first section only.

    Is there a way to add them to all non empty sections before creating the split?

    If not, it's not really a big deal since the macro you posted preserves the formatting of the headers and I can add them once they are split*


    When they are split though, the blank/empty sections are still there

    I can delete them from the newly generated files. I can't find the code to delete variable pages though

    In my post here msofficeforums[dot]com/word-vba/46254-deleting-specific-pages-word-vba[dot]html#post156658 I am trying to work out how to delete the pages returned as 'blank'

    Thanks for the link.

    I will get a larger sample of multi invoice files and test it on these to see whether they are similarly formatted.

    In the meantime, the code you have posted is definitely useful
    thanks

    *I have added the headers and footers manually (i.e. section by section) before running the code that you have given me. The headers and footers disappear from all but the first section when the document is split though
    Last edited by Lefemmenikit; 01-01-2021 at 10:45 PM. Reason: tested again

  4. #4
    Hi Gmayor

    thank you for the code

    i have tested it with no issues

    however, when I tested the same code on a colleagues’s word doc(which has the same version of Windows, office,etc)

    the code runs into an error with the following line:
    subdoc.range.paste

    why would that be the case? Pressing ‘continue’ in vba allows the rest of the code to run normally

    thanks

  5. #5
    Quote Originally Posted by Lefemmenikit View Post
    Hi Gmayor


    the code runs into an error with the following line:
    subdoc.range.paste

    why would that be the case? Pressing ‘continue’ in vba allows the rest of the code to run normally
    I found the cause if anyone else is running into the same problem:



    The other user had the clipboard history button seen in the screencap above set to 'on'. Once this was switched to 'off' the code ran smoothly with no issues

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •