Creating a new header for current section using VBA RRS feed

  • Question

  • Hi Everyone,

    I have written a code in Excel VBA (German user interface) to create a word doc and publish my calculations performed in Excel doc. I have some problems with formatting this document. I want to have seperate headers for different sections of the word document. For this reason, I have created section breaks, unlinked the header from previous section and have tried to create a new one... But the header is being created for the previous section and not the current section. The following snippet shows the code I've used.

    With MS_Word.Selection      
            .InsertBreak Type:=wdSectionBreakNextPage
            .GoTo what:=wdGoToLine, which:=wdGoToLast
            MS_Word.Application.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
            .HeaderFooter.LinkToPrevious = False
            .EndKey Unit:=wdLine, Extend:=wdExtend
            .ParagraphFormat.Alignment = wdAlignParagraphRight
            If MS_Word.Language = msoLanguageIDGerman Then
            .Fields.Add Range:=.Range, Type:=wdFieldStyleRef, Text:="""Überschrift 1""", PreserveFormatting:=True
            .Fields.Add Range:=.Range, Type:=wdFieldStyleRef, Text:="""Heading 1""", PreserveFormatting:=True
            End If
            .TypeText Text:=vbTab
            .TypeText Text:=vbTab
            .TypeText Text:="Seite "
            .Fields.Add Range:=.Range, Type:=wdFieldPage
            .TypeText Text:=" von "
            .Fields.Add Range:=.Range, Type:=wdFieldNumPages
            MS_Word.Application.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    end With 

    In the previous section, I have a simple text as a header and in the current one, I want to use a dynamic header with fields. After the execution of this code, the opposite thing of what I wanted to have has happened i.e., the header from the previous section is replaced with the one with the fields and in the current section, the simple text header is retained.

    I have checked the code with Macrorecorder and it also used the same commands.

    Does anyone have an idea what went wrong here?

    Thanks and Regards,


    Tuesday, February 26, 2013 9:50 AM


All replies