none
ms word automation header and footer RRS feed

  • Question

  • I am automating msword 2007 using c# and a template.

    How can I insert headers and footers in the word document with images, text and page numbers.
    these codes are not working properly:

    foreach (Word.Section section in oDoc.Sections)
    {
    section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = "";
    section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "Page";
    section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range..InlineShapes.AddPicture FileName:="D:\footer.jpg"

    section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range..InlineShapes.AddPicture FileName:="D:\header.jpg"
    }

    I want to have page number, text, and image at the footer and at the same time have a continous page numbering throughout the document, but all of these text, page numbers, and images should NOT appear on every FIRST page of every nextpage section breaks.

    i hope i have clearly explained my situation ~x( I don't know what to do

    First page of every sectionbreaks don't need any header and footer but the document should have the right page numbering all throughout.

    Thanks in advance for your help
    Tuesday, January 4, 2011 12:00 AM

Answers

  • Hi HmFG,

    Thank you for posting and we are glad to help with you.

    After reading your post, I knew that you want to insert page number, text and image in the header and footer and want page number to be continuous. But I am not clear about what the meaning of "images should NOT appear on every FIRST page of every nextpage section breaks." If I have misunderstood you, please let me know.

    As far as I know, you can set use Selection.Fields.Add method to set the page number to be continuously, and below is the code which I modified:

    static void Main(string[] args)
            {
                
    string fileDir = Environment
    .CurrentDirectory;
                
    if (fileDir.EndsWith("\\bin\\Debug"
    ))
                {
                    fileDir = fileDir.Replace(
    "bin\\Debug"""
    );
                }
                
    string fileName = "SectionBreak.docx"
    ;
                
    Application app = new Application
    ();
                
    Document doc = null
    ;
                app.Visible = 
    true
    ;
                doc = app.Documents.Open(fileDir + fileName);
                InsertHeaderAndFooter(doc);
            }
     
            
    private static void InsertHeaderAndFooter(Document
     oDoc)
            {
                
    // (int)section.Range.get_Information(WdInformation.wdActiveEndSectionNumber)

                
    foreach (Section section in oDoc.Sections)
                {
                    section.Footers[
    WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = ""
    ;
                    
    //section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "Page" + section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Count;

                    section.Footers[
    WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InlineShapes.AddPicture("D:\\footer.jpg");
                    section.Headers[
    WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InlineShapes.AddPicture("D:\\header.jpg"
    );
                }
                
    // Open up the footer in the word document

                oDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.
    WdSeekView.wdSeekCurrentPageFooter;
     
                
    // Set current Paragraph Alignment to Center

                oDoc.ActiveWindow.ActivePane.Selection.Paragraphs.Alignment = 
    WdParagraphAlignment.wdAlignParagraphCenter;
     
                
    // Type in 'Page '

                oDoc.ActiveWindow.Selection.TypeText(
    "Page ");
     
                
    // Add in current page field

                
    Object CurrentPage = WdFieldType.wdFieldPage;
                oDoc.ActiveWindow.Selection.Fields.Add(oDoc.ActiveWindow.Selection.Range,
                    
    ref CurrentPage, Type.Missing, Type
    .Missing);
     
                
    // Type in ' of '

                oDoc.ActiveWindow.Selection.TypeText(
    " of ");
     
                
    // Add in total page field

                
    Object TotalPages = WdFieldType.wdFieldNumPages;
                oDoc.ActiveWindow.Selection.Fields.Add(oDoc.ActiveWindow.Selection.Range,
                    
    ref TotalPages, Type.Missing, Type
    .Missing);
            }

    I studied this from this site:http://social.msdn.microsoft.com/Forums/en/vsto/thread/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8

    I hope this can help you and feel free to follow up.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Tuesday, January 11, 2011 6:00 AM
    Thursday, January 6, 2011 3:48 AM
  • Hi hmFG

    <<I want to have page number, text, and image at the footer and at the same time have a continous page numbering throughout the document, but all of these text, page numbers, and images should NOT appear on every FIRST page of every nextpage section breaks.>>

    Set the DifferentFirstPage property of Section.PageSetup to TRUE.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Tuesday, January 11, 2011 6:01 AM
    Thursday, January 6, 2011 10:16 AM
    Moderator

All replies

  • I am automating ms word 2007 using c# and a template.

    How can I insert headers and footers in the word document with images, text and page numbers.
    these codes are not working properly:

    foreach (Word.Section section in oDoc.Sections)
    {
    section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = "";
    section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "Page";
    section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range..InlineShapes.AddPicture FileName:="D:\footer.jpg"

    section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range..InlineShapes.AddPicture FileName:="D:\header.jpg"
    }

    I want to have page number, text, and image at the footer and at the same time have a continous page numbering throughout the document, but all of these text, page numbers, and images should NOT appear on every FIRST page of every nextpage section breaks.

    i hope i have clearly explained my situation ~x( I don't know what to do

    First page of every sectionbreaks don't need any header and footer but the document should have the right page numbering all throughout.

    Thanks in advance for your help
    • Moved by Larcolais Gong Thursday, January 6, 2011 8:50 AM (From:Visual C# General)
    • Merged by Bruce Song Friday, January 7, 2011 2:24 AM duplicated
    Monday, January 3, 2011 8:01 PM
  • Off-topic here. Try http://social.msdn.microsoft.com/Forums/eu/worddev/threads instead.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Monday, January 3, 2011 9:47 PM
  • Hi HmFG,

    Thank you for posting and we are glad to help with you.

    After reading your post, I knew that you want to insert page number, text and image in the header and footer and want page number to be continuous. But I am not clear about what the meaning of "images should NOT appear on every FIRST page of every nextpage section breaks." If I have misunderstood you, please let me know.

    As far as I know, you can set use Selection.Fields.Add method to set the page number to be continuously, and below is the code which I modified:

    static void Main(string[] args)
            {
                
    string fileDir = Environment
    .CurrentDirectory;
                
    if (fileDir.EndsWith("\\bin\\Debug"
    ))
                {
                    fileDir = fileDir.Replace(
    "bin\\Debug"""
    );
                }
                
    string fileName = "SectionBreak.docx"
    ;
                
    Application app = new Application
    ();
                
    Document doc = null
    ;
                app.Visible = 
    true
    ;
                doc = app.Documents.Open(fileDir + fileName);
                InsertHeaderAndFooter(doc);
            }
     
            
    private static void InsertHeaderAndFooter(Document
     oDoc)
            {
                
    // (int)section.Range.get_Information(WdInformation.wdActiveEndSectionNumber)

                
    foreach (Section section in oDoc.Sections)
                {
                    section.Footers[
    WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = ""
    ;
                    
    //section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "Page" + section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Count;

                    section.Footers[
    WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InlineShapes.AddPicture("D:\\footer.jpg");
                    section.Headers[
    WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InlineShapes.AddPicture("D:\\header.jpg"
    );
                }
                
    // Open up the footer in the word document

                oDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.
    WdSeekView.wdSeekCurrentPageFooter;
     
                
    // Set current Paragraph Alignment to Center

                oDoc.ActiveWindow.ActivePane.Selection.Paragraphs.Alignment = 
    WdParagraphAlignment.wdAlignParagraphCenter;
     
                
    // Type in 'Page '

                oDoc.ActiveWindow.Selection.TypeText(
    "Page ");
     
                
    // Add in current page field

                
    Object CurrentPage = WdFieldType.wdFieldPage;
                oDoc.ActiveWindow.Selection.Fields.Add(oDoc.ActiveWindow.Selection.Range,
                    
    ref CurrentPage, Type.Missing, Type
    .Missing);
     
                
    // Type in ' of '

                oDoc.ActiveWindow.Selection.TypeText(
    " of ");
     
                
    // Add in total page field

                
    Object TotalPages = WdFieldType.wdFieldNumPages;
                oDoc.ActiveWindow.Selection.Fields.Add(oDoc.ActiveWindow.Selection.Range,
                    
    ref TotalPages, Type.Missing, Type
    .Missing);
            }

    I studied this from this site:http://social.msdn.microsoft.com/Forums/en/vsto/thread/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8

    I hope this can help you and feel free to follow up.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Tuesday, January 11, 2011 6:00 AM
    Thursday, January 6, 2011 3:48 AM
  • Hi hmFG

    <<I want to have page number, text, and image at the footer and at the same time have a continous page numbering throughout the document, but all of these text, page numbers, and images should NOT appear on every FIRST page of every nextpage section breaks.>>

    Set the DifferentFirstPage property of Section.PageSetup to TRUE.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Tuesday, January 11, 2011 6:01 AM
    Thursday, January 6, 2011 10:16 AM
    Moderator
  • I used the given code above. The number of sections in the document is  equal to the number of image footer and header inserted on each page, how can i fix this problem?

    Thank you

    Wednesday, January 12, 2011 1:00 AM