Changing footers when concatenating documents programmatically

J

John Lott

Using Word 2003, the interop assembly, and .net, I am concatenating documents
together.

In order to change the footer from say A-1 for appendix A, to the next B-1
for appendix B, I am having to change the page orientation and then change it
back after the new footer is accepted.

This is a workaround, does anyone know a better way.

Thanks

Code below

Public Sub AppendFile(ByVal StylePathandFile As String, ByVal
InputFileandName As String, ByVal PageBreakRequired As Boolean) Implements
rptAFERDInterfCore.IDescribeOutputFile.AppendFile
'WES todo"
'Convert the input into the doc file
Dim localDocument As Word.Document = Nothing
Dim NewSection As Word.Section
Dim LastSection As Word.Section
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim Restart As Boolean
Dim Same As Boolean

mApplication.Selection.EndOf(CType(Word.WdUnits.wdStory, Object))

Try
localDocument =
CType(mApplication.Documents.Open(CType(InputFileandName, Object)),
Word.Document)

ClearBeginningPageBreak(localDocument)

NewSection = CType(localDocument.Sections(1), Word.Section)

Restart =
NewSection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection

Catch ex As Exception

Finally
localDocument.Close(CType(False, Object))
End Try

If Restart And Not mFirst Then
PageBreakRequired = False
End If

If PageBreakRequired Then
mDocument.Activate()
If Not HasAPageBreak(mDocument) Then

mApplication.Selection.InsertBreak(CType(Word.WdBreakType.wdSectionBreakNextPage, Object))
mApplication.Selection.EndOf(CType(Word.WdUnits.wdStory,
Object))
End If
End If

Try

localDocument =
CType(mApplication.Documents.Open(CType(InputFileandName, Object)),
Word.Document)

ClearBeginningPageBreak(localDocument)

Try

b = localDocument.Sections.Count

For a = 1 To b

c = mDocument.Sections.Count

LastSection = CType(mDocument.Sections(c), Word.Section)

NewSection = CType(localDocument.Sections(a),
Word.Section)

If Restart And Not mFirst Then
If NewSection.PageSetup.Orientation =
Word.WdOrientation.wdOrientPortrait Then
NewSection.PageSetup.Orientation =
Word.WdOrientation.wdOrientLandscape
Else
NewSection.PageSetup.Orientation =
Word.WdOrientation.wdOrientPortrait
End If

End If

If PageBreakRequired Then

Else

Same = CompareSections(LastSection.PageSetup,
NewSection.PageSetup)

mDocument.Activate()
If Same Then

mApplication.Selection.InsertBreak(CType(Word.WdBreakType.wdSectionBreakContinuous, Object))
Else

mApplication.Selection.InsertBreak(CType(Word.WdBreakType.wdSectionBreakNextPage, Object))
End If
End If

NewSection.Range.Copy()

mDocument.Activate()

mApplication.Selection.EndOf(CType(Word.WdUnits.wdStory,
Object))

mApplication.Selection.Paste()

c = mDocument.Sections.Count

LastSection = mDocument.Sections(c)


LastSection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).LinkToPrevious = Not Restart

LastSection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection = Restart

If Restart And Not mFirst Then
If LastSection.PageSetup.Orientation =
Word.WdOrientation.wdOrientPortrait Then
LastSection.PageSetup.Orientation =
Word.WdOrientation.wdOrientLandscape
Else
LastSection.PageSetup.Orientation =
Word.WdOrientation.wdOrientPortrait
End If
End If
mFirst = False
Next
 
C

Cindy M.

Hi =?Utf-8?B?Sm9obiBMb3R0?=,
Using Word 2003, the interop assembly, and .net, I am concatenating documents
together.

In order to change the footer from say A-1 for appendix A, to the next B-1
for appendix B, I am having to change the page orientation and then change it
back after the new footer is accepted.
What's the problem if you don't do this?

Have you considered using Word's built-in functionality for automatically
including "chapter" numbering with the page number?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
J

John Lott

This report consists of about 45 word documents. These documents comprise
about 4 chapters and 3 indexes. So in theory a single chapter could consists
of 1 to xx sub-documents.
 

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