none
Combine multiple word files and keep the headings of the original files RRS feed

  • Question

  • I am combining multiple word files in an application.  I am using Word 2010.   This process works fine. However the heading of the first document is repeated for all of the remaining documents in the merged file.  I would like each file's original header and footer to stay with it in the merged file.

    For the heck of it I tried doing the same thing within the Word application manually and the same thing occurs.  Some of the files have protection enabled.  Even when I disable the protection the problem still occurs.

    Has anyone run into this before?

    Thanks,

    Mark

    This is the code I am using

     Dim newDoc As Word.Document = oWord.Documents.Add()
               cPara = newDoc.Content.Paragraphs.Add(newDoc.Bookmarks.Item("\endofdoc").Range)
                cPara.Range.Text = ""
    
                'loop thru the file list and add them to the combined file
                While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        Dim curend As String
                        curend = currentRow(1)
                             ' set the control \ cursor to the end of the document to insert the next endorsement
                            cPara.Range.InsertFile(curend)
                            'Set the cursor at the end of file 
                            cPara.Range.Characters.Last.Select()
                            cPara.Range.Collapse()
                            If Not MyReader.EndOfData Then
                                'Insert Page break
                                  cPara.Range.InsertBreak(Type:=7)
                                cPara.Range.InsertBreak(Type:=2)
    End While

    Wednesday, June 3, 2015 4:07 PM

Answers

  • Hi Matt

    From your problem description I'm guessing you mean "Headers", as in Headers and Footers, text that appears at the top/bottom of every page? ("Headings" are things like chapter and section titles that provide internal organization in document text. As Word uses both terms, it helps to keep them straight when asking for assistance :-))

    The reason you're seeing this behavior is because Headers and Footers are section-specific and can only be changed by inserting a SECTION BREAK and then unlinking the new section from the previous one - all that before inserting the next file.

    If you want to see this at work, switch to the Outline view, and in the "Master Document" tab of the Ribbon click "Show Document" (this enables the Master Document functionality for the document), then use the "Insert" button to bring in a file. You should see that Word inserts section breaks and that the inserted file(s) retain their headers and footers.

    While you could use this functionality, there are certain limitations connected with it that can make the resulting combined document unstable for further editing (it's fine for printing). If you need a document that people will be working with you can adjust your macro. From the code you show us, it appears you're already inserting a NextPage type of section break. All that remains, then, would be to UNLINK the new section's headers/footers from the previous one.  Something like this:

    NewDoc.Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Sagebrush123 Friday, June 5, 2015 3:22 AM
    Wednesday, June 3, 2015 5:12 PM
    Moderator
  • Hi Mark

    Hmmm. See if the following helps:

    Page Layout tab.
    Click the dialog launcher in the Page Setup group.
    On the "Layout" tab of the dialog box, check the setting in the "Section start" dropdown. If this is NOT "New Page", select that.

    Now try again.

    I suspect this may be set to "Continuous"...?


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Sagebrush123 Monday, June 15, 2015 9:13 PM
    Friday, June 12, 2015 2:32 PM
    Moderator

All replies

  • Hi Matt

    From your problem description I'm guessing you mean "Headers", as in Headers and Footers, text that appears at the top/bottom of every page? ("Headings" are things like chapter and section titles that provide internal organization in document text. As Word uses both terms, it helps to keep them straight when asking for assistance :-))

    The reason you're seeing this behavior is because Headers and Footers are section-specific and can only be changed by inserting a SECTION BREAK and then unlinking the new section from the previous one - all that before inserting the next file.

    If you want to see this at work, switch to the Outline view, and in the "Master Document" tab of the Ribbon click "Show Document" (this enables the Master Document functionality for the document), then use the "Insert" button to bring in a file. You should see that Word inserts section breaks and that the inserted file(s) retain their headers and footers.

    While you could use this functionality, there are certain limitations connected with it that can make the resulting combined document unstable for further editing (it's fine for printing). If you need a document that people will be working with you can adjust your macro. From the code you show us, it appears you're already inserting a NextPage type of section break. All that remains, then, would be to UNLINK the new section's headers/footers from the previous one.  Something like this:

    NewDoc.Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Sagebrush123 Friday, June 5, 2015 3:22 AM
    Wednesday, June 3, 2015 5:12 PM
    Moderator
  • Cindy, Your suggestion does help. I ran into another problem when I read in the file. For example the footer on the original file displays the text "ca 123 90 01 76". When I insert the original file into the new file the footer in the new file shows a copyright notice in the footer instead of the previous text. Weird. I tried to delete the footer and put in the text "ca 123 90 01 76" myself which works out fine. Now, I am having a hard time figuring out how to put in a page numbering sequence " Page X of Y". Where each section would start with its own number 1. Thanks, Mark
    Friday, June 5, 2015 3:22 AM
  • Cindy,

    The link to previous is working.  I am combining anywhere from 10 to 20 documents. The

    .LinkToPrevious = false

    works on some documents but not on others. Any hints as to what I need to look for?

    Thanks,

    Mark

    Friday, June 5, 2015 5:03 PM
  • I am combining multiple Word files into one. I had a problem maintaining header info but Cindy provided me with this command that helped out (newDoc.Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False.

    I am using VS Community 2013, vb.net and Word 2010.  Most files retain their headers.  But some still stay as LinkToPrevioous=True even though I set it to False.  My code is attached.  I noticed that some files have SectionBreak(continuous) in them.  Do I need to be concerned about these additional Section Breaks in my code?  Is there a way to find out why setting the Link to False isn't working?

    Thanks,

    Mark

    Dim curSection As Integer = 1
     While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        ' Let's get the next file to merge 
                        curEndorsement = currentRow(1)		' current file to insert
                            Dim wdHeaderFooterPrimary As Integer = 1    'standard Microsoft enumerated values
                            If curSection = 1 Then	'if on the 1st doc Open it instead of creating a new one
                                newDoc = oWord.Documents.Open(curEndorsement)
                                newDoc.SaveAs2(tempPath + "Policy" + gPolicyID + ".docx", Word.WdSaveFormat.wdFormatDocumentDefault)
                            Else
                                    newDoc.Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
                                    newDoc.Application.Selection.InsertFile(curEndorsement)
                            End If
                            newDoc.Activate()
                            'Set the cursor at the end of file 
                            newDoc.Application.Selection.EndKey(WdUnits.wdStory, Nothing)
                            newDoc.Application.Selection.Select()
                            newDoc.Application.Selection.Collapse()
                            If Not MyReader.EndOfData Then
                                'Insert Page break
                                newDoc.Application.Selection.InsertBreak(Type:=2)
                                 newDoc.Application.Selection.PageSetup.TextColumns().SetCount(NumColumns:=1)
                                curSection += 1     ' increment the current section we are in
                            End If
                        End If
    				Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                End While
    

    Friday, June 5, 2015 8:32 PM
  • Hi Mark

    <<.LinkToPrevious = false

    works on some documents but not on others. Any hints as to what I need to look for?>>

    One possible reason would be "Different First Page" being activated, or "Odd/Even". Can you describe HOW it doesn't work in more detail?

    << Now, I am having a hard time figuring out how to put in a page numbering sequence " Page X of Y". Where each section would start with its own number 1.>>

    Look at the Help topic RestartNumberingAtSection


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, June 6, 2015 6:29 PM
    Moderator
  • Cindy, I did more research into this after my last question. Most files that I am combining have these fields checked. Different first page OddEvenPages The file that I had the problem on only had this field checked; OddEvenPages. I am agreeing it has something to do with these fields. So, on the problem file, I checked Different Firstpage and left OddEvenPages checked and tried to combine them again. But when I did this the header on page 1 disappeared. I put in my own header to see what would happen. In that case the file that I checked, Different Firstpage on worked fine. But the file I inserted after it showed the header for the file before it not its own header. My next step is just to use new documents and keep testing the impact of checking these fields and merging files with headers. Mark
    Saturday, June 6, 2015 7:27 PM
  • Hi Mark

    The way to take care of this is to include all three variations:

     newDoc.Sections.Last.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
     newDoc.Sections.Last.Headers(wdHeaderFooterEvenPages).LinkToPrevious = False
     newDoc.Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, June 6, 2015 7:51 PM
    Moderator
  • I just tried your settings but still no luck. 

    I think it has to do with the page setup and the above fields.  I did some testing.  I created 2 new docs.  See below:

    doc 1  DifferentFirst page is unchecked (Word had it defaulted this way)

              DifferentOddEven   unchecked  (Word had it defaulted this way)

             Placed some text in the header

    Doc 2   DifferentFirst page is checked

              DifferentOddEven   checked

             Placed different text in the header

    Manually I loaded doc 1.  Then;

      Went to Page Layout and inserted  Section Break-Next page

      Went to Headers and clicked off Link to Previous.

       Then deleted header in doc 1.

        Went to Insert Menu and went to Object-Text from file and

       Inserted doc 2.

    Result of combined file:

       The header text on doc 2 was the same as doc 1 (but shouldn't be)

    The fields on doc2 in the combined document were changed;

             DifferentFirst page was unchecked (original document has this checked)

              DifferentOddEven   was unchecked  (original document has this checked)

    This must be where my problem lies.  How do I change the above setting before I load the file

    Thanks for your help,

    Mark

      


    Saturday, June 6, 2015 8:12 PM
  • Hi Mark

    <click>The reason for what you're seeing is that the information is stored in the Section Break. The last (or, if a document has but 1 section) is the last paragraph mark. When you use InsertFile, this last paragraph mark is cut off so that the document being imported conforms to the current document. This was a design decision for the Word application that was made very early on and makes sense in scenarios when you're assembling a document from many pieces. (As opposed to combining multiple documents that should keep their identities.)

    Sorry that slipped my mind before <sigh>

    What you'd need to do is insert a section break at the end of each document you're inserting.

    You might want to check out the Master Document functionality, located in the Outline View. Click "Show Document", then the Insert button that will appear. This functionality is fine if you're assembling a document for printing. If it's to be used for anything else - especially editing - you'd want to remove the extra section breaks that are inserted and "unlink" the sub-documents from the original files (which will incorporate them as static text in the main document).


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, June 7, 2015 7:23 PM
    Moderator
  • Cindy, I thought I was adding the section break at the end of each document already. Can you look at my last section of code I sent? My program inserts a file, goes to the end of the file and then inserts a Section Break-New Page. Does this need to be done differently with a continuous section break maybe? Thanks, Mark
    Monday, June 8, 2015 3:48 AM
  • Hi Mark

    You're inserting the section break in the target file. But in order to preserve the settings coming in in the last (only) section of the inserted file the section break must be in the file you're INSERTING.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 9, 2015 5:18 PM
    Moderator
  • Cindy,

    I reviewed your answer. I opened each file manually within the Word app and went to the end of the document and inserted a Continuous section break. Then saved each file independently.  I then ran the program that inserted the files into a target file.  The headers were still incorrect.  You mentioned "last(only)" .  Does this signify something else I need to do?  I must still be doing something wrong.

    Thanks,

    Mark

    Tuesday, June 9, 2015 8:50 PM
  • Hi Mark

    "Last (only)" refers to the fact that every document will have at least one section break. The information is stored in the last paragrpah mark of the document for the last section or, if there's only one section, for the entire document.

    Did you test the Master Document functionality, mentioned in an earlier reply? Does that give you the expected/desired result?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, June 10, 2015 5:34 PM
    Moderator
  • Cindy, I haven't tried the master document. I will try that next. What I did do and seems to be working is as follows; Went to each original document and forced all headers on it to be the same. I did this by going to each header and copying what was in the header, Then I went to Different First Page and checked it or unchecked it depending on its current setting. Then deleted what was in the header and pasted in what I had just copied. Then I went to the Odd and Even Pages and did the same thing. So far this seems to work. But I started reading up on the Master Document and will try that next. The documents I merge in could be different for each customer. Do you think the Master document would work in that Situation? Thanks, Mark
    Wednesday, June 10, 2015 6:32 PM
  • Cindy,

    I tried the Master Document but I am still experiencing problems.  I read in each file and some come in with Section Break Continuous followed by Section Break(Next Page) Immediately following it. These are both at the end of the document.

    Some pages come in with just Section Break Continuous at the end.  Odd why it isn't the same for each document.

    If I correct each Section Break Continuous Manually (By changing them to New Page) in the Word App., the Master Document comes out correct. 

    Attached is the code I am using.  Is there something wrong in it that is causing the difference in the Section Breaks?

    Thanks,

    MArk

    While Not MyReader.EndOfData
    
                    Try
                        currentRow = MyReader.ReadFields()
                        ' Let's get the next file to merge 
                        curEndorsement = currentRow.GetValue(1)
                        If File.Exists(curEndorsement) = False Then
    						' do this
                        Else
                            If curSection = 1 Then      'if this is the first file Open It don't add it
                                newDoc = oWord.Documents.Open(curEndorsement)
                                oWord.ActiveWindow.ActivePane.View.Type = 2         'wdOutlineView 
                                newDoc.Application.Selection.Range.Subdocuments.Expanded = True
                                ' go to the last line
                                newDoc.Application.Selection.EndKey(WdUnits.wdStory, Nothing)
                                oWord.ActiveWindow.View.Zoom.Percentage = 90
                                newDoc.SaveAs2(tempPath + "Policy" + gPolicyID + ".docx", Word.WdSaveFormat.wdFormatDocumentDefault)
                            Else
                                newDoc.Activate()
                                '  Insert the file
                                newDoc.Application.Selection.Range.Subdocuments.AddFromFile(curEndorsement, False, False, , , False, , )
                            End If
                            If Not MyReader.EndOfData Then
                                curSection += 1     ' increment the current section we are in
                                TextBox1.Text = "Combining " + Convert.ToString(curSection) + " out of " + Convert.ToString(lineCount) + " files."
                            End If
                        End If
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                    TextBox1.Text = "Combining " + Convert.ToString(curSection) + " out of " + Convert.ToString(lineCount)
                End While

    Mark

    Thursday, June 11, 2015 7:12 PM
  • Hi Mark

    Hmmm. See if the following helps:

    Page Layout tab.
    Click the dialog launcher in the Page Setup group.
    On the "Layout" tab of the dialog box, check the setting in the "Section start" dropdown. If this is NOT "New Page", select that.

    Now try again.

    I suspect this may be set to "Continuous"...?


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Sagebrush123 Monday, June 15, 2015 9:13 PM
    Friday, June 12, 2015 2:32 PM
    Moderator
  • Cindy,

    That resolved the last issue.  The main process to follow to merge unlike files is to use the Master Document feature.

    Then on each source file,  go to the "Layout" tab of the dialog box, check the setting in the "Section start" dropdown. If this is NOT "New Page", change it to New Page.

    If the file has Protection Enabled, the protection must be stopped by going to the Review Menu and then the Restrict Editing Icon.

    Thanks for your patience and help

    Mark

    • Marked as answer by Sagebrush123 Monday, June 15, 2015 9:13 PM
    • Unmarked as answer by Sagebrush123 Monday, June 15, 2015 9:13 PM
    Monday, June 15, 2015 9:13 PM