I have been going in circles trying to get this code to work. When I combine documents, I need to have the headers and footers stay intact in the destination document when copied from the source document. The headers and footers are not too fancy, but each document will have an individual primary header and/or footer. When I use this combining code, the footers keep spilling into the next appended document. And if a source document has a blank header or footer, it should stay that way. Please help… I have several different attempts inside this code. Also, the styles I am using will be consistent, so the issue is just the headers and footers. Also, I will not have a different first page header or footer.
Also, is there a way to add the page break without using the DocumentBuilder? In a previous post, you stated that I should not use the DocumentBuilder for this process, but the page break is necessary.
Private Sub AppendDoc(ByVal dstDoc As Document, ByVal srcDoc As Document)
Dim dstdocBuilder As DocumentBuilder = New DocumentBuilder(dstDoc)
Try
Dim i As Integer = 0
Do While i < srcDoc.Sections.Count
' Add a page break first.
If i = 0 Then
dstdocBuilder.MoveToDocumentEnd()
dstdocBuilder.InsertBreak(BreakType.SectionBreakContinuous)
dstdocBuilder.InsertBreak(BreakType.PageBreak)
End If
If i = 0 Then
'Dim srcsection As Section = srcDoc.Sections(srcDoc.Sections.Count - 1)
'Dim srcPrimaryHeader As HeaderFooter = srcsection.HeadersFooters(HeaderFooterType.HeaderPrimary)
'If Not srcPrimaryHeader Is Nothing Then srcPrimaryHeader.IsLinkedToPrevious = False
'Dim srcPrimaryFooter As HeaderFooter = srcsection.HeadersFooters(HeaderFooterType.FooterPrimary)
'If Not srcPrimaryFooter Is Nothing Then srcPrimaryFooter.IsLinkedToPrevious = False
'Dim srcdocBuilder As DocumentBuilder = New DocumentBuilder(srcDoc)
'srcdocBuilder.MoveToDocumentEnd()
'srcdocBuilder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary)
End If
Dim section As Section = CType(dstDoc.ImportNode(srcDoc.Sections(i), True, ImportFormatMode.KeepSourceFormatting), Section)
dstDoc.Sections.Add(section)
If i = 0 Then
'Dim primaryHeader As HeaderFooter = section.HeadersFooters(HeaderFooterType.HeaderPrimary)
'If Not primaryHeader Is Nothing Then primaryHeader.IsLinkedToPrevious = False
'Dim primaryFooter As HeaderFooter = section.HeadersFooters(HeaderFooterType.FooterPrimary)
'If Not primaryFooter Is Nothing Then primaryFooter.IsLinkedToPrevious = False
Dim dstPrimaryFooter As HeaderFooter = dstDoc.Sections(dstDoc.Sections.Count - 1).HeadersFooters(HeaderFooterType.FooterPrimary)
If Not dstPrimaryFooter Is Nothing Then dstPrimaryFooter.IsLinkedToPrevious = False
End If
section.PageSetup.SectionStart = SectionStart.Continuous
For Each paragraph As Paragraph In section.GetChildNodes(NodeType.Paragraph, True)
paragraph.ParagraphFormat.WidowControl = True
Next paragraph
i += 1
Loop
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Try
End Sub