none
Headers on even and odd pages, VSTO RRS feed

  • Question

  • Hi,

    I'm trying to crate word document. I created that header

    MIDDLE-Text

    Left header site Page 1 of 4

    ______________________________________________

    but i want different headers at even and odd pages. My even hooter should looks like that but Left Table Text should be switched with Page 1 of 4. But when i did it Bottom Border go to footer... Conclusion : now i have odd headers like that and even switched but bottom border at footer. How can i fix even header?

    Code which i use to create odd Headers

    foreach (Word.Section section in wordDocument.Sections)
                {
                    object TotalPages = Word.WdFieldType.wdFieldNumPages;
                    object CurrentPage = Word.WdFieldType.wdFieldPage;
                    wordApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
                    wordApplication.ActiveWindow.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                    wordApplication.ActiveWindow.Selection.Font.Size = 10;
                    wordApplication.ActiveWindow.Selection.TypeText("MIDDLE-TEXT");
                    wordApplication.ActiveWindow.Selection.Tables.Add(wordApplication.Selection.Range, 1, 2, false, true);
                    wordApplication.ActiveWindow.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    wordApplication.ActiveWindow.Selection.Font.Size = 8;
                    wordApplication.ActiveWindow.Selection.TypeText("Left Table Text\nCompany address and so on");
                    wordApplication.ActiveWindow.Selection.MoveRight(Word.WdUnits.wdCharacter, 1, ref oMissing);
                    wordApplication.ActiveWindow.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                    wordApplication.ActiveWindow.Selection.Font.Size = 8;
                    wordApplication.ActiveWindow.Selection.TypeText("Page ");
                    wordApplication.ActiveWindow.Selection.Fields.Add(wordApplication.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);
                    wordApplication.ActiveWindow.Selection.TypeText(" of ");
                    wordApplication.ActiveWindow.Selection.Fields.Add(wordApplication.Selection.Range, ref TotalPages, ref oMissing, ref oMissing);
                    wordApplication.ActiveWindow.Selection.MoveDown(Word.WdUnits.wdLine, 1, ref oMissing);
                    wordApplication.ActiveWindow.Selection.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    wordApplication.ActiveWindow.Selection.Borders[Word.WdBorderType.wdBorderBottom].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
                    wordApplication.ActiveWindow.Selection.Borders[Word.WdBorderType.wdBorderBottom].Color = Word.WdColor.wdColorBlack;
                    wordApplication.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
                }

    Thanks for each advice.

    Wednesday, January 28, 2015 9:52 AM

Answers

  • Hi mikroice

    There is a problem with Word when you try to use macro recorder code to manipulate headers/footers. Word doesn't always act on the Header/Footer you think should be the active one. This has been a problem since the introduction of VBA in Word (the object model you're using).

    The solution is to NOT work on the current selection, but on the underlying objects, meaning you need to remove the SeekView lines, create a RANGE object corresponding to the header or footer you want to address and edit all the ActiveWindow.Selection lines to work with that Range object.

    For the (pseudo)code snippet that follows, I'm going to assume that you want to work with the Header of Section 1 in the current document:

       Word.Range rngHeaderEven = wordDocument.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range;
       Word.Range rngHeaderOdd = wordDocument.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range; 
      rngHeaderEven.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;



    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, February 12, 2015 8:51 PM
    Moderator

All replies

  • Hi mikroice,

    >>but i want different headers at even and odd pages. My even hooter should looks like that but Left Table Text should be switched with Page 1 of 4. But when i did it Bottom Border go to footer... Conclusion : now i have odd headers like that and even switched but bottom border at footer. How can i fix even header?<<

    If I understood correctly, you want to set the page headers for the even page. To make the pages headers different between with odd and even pages, we need to set the options like figure below:

    Then we can set the page header for the even pages and here are a sample to set this option and set the page header for the even pages:

    ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter = True
    ActiveWindow.ActivePane.View.SeekView = wdSeekEvenPagesHeader
    Selection.TypeText Text:="My Event Page Header"
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 29, 2015 3:28 AM
    Moderator
  • I know how to add different headers or footers at even and odd pages. My problem is why the same header (i switched left and right side only) didnt fit at odd pages and move some part of it to footer?
    Monday, February 2, 2015 10:03 AM
  • Hi mikorice,

    >>I know how to add different headers or footers at even and odd pages. My problem is why the same header (i switched left and right side only) didnt fit at odd pages and move some part of it to footer?<<

    I am try to reproduce the issue however failed. After I ran the code on the orignal post, all the headers were set correct. Here is the fingure for your reference:

    I am testing the code in Word 2013, which the version of Word are you using? Did I miss any steps?

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 4, 2015 8:36 AM
    Moderator
  • Hi mikroice

    There is a problem with Word when you try to use macro recorder code to manipulate headers/footers. Word doesn't always act on the Header/Footer you think should be the active one. This has been a problem since the introduction of VBA in Word (the object model you're using).

    The solution is to NOT work on the current selection, but on the underlying objects, meaning you need to remove the SeekView lines, create a RANGE object corresponding to the header or footer you want to address and edit all the ActiveWindow.Selection lines to work with that Range object.

    For the (pseudo)code snippet that follows, I'm going to assume that you want to work with the Header of Section 1 in the current document:

       Word.Range rngHeaderEven = wordDocument.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range;
       Word.Range rngHeaderOdd = wordDocument.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range; 
      rngHeaderEven.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;



    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, February 12, 2015 8:51 PM
    Moderator