none
How to create a header of built in style'Austere(Odd)' in Microsoft.Office.Interop.Word(c#)? RRS feed

  • Question

  • How to create a header of built in  style'Austere(Odd)' in Microsoft.Office.Interop.Word(c#)?

    • Edited by anvin v paulose Thursday, May 17, 2012 9:49 AM
    • Moved by CoolDadTx Thursday, May 17, 2012 2:11 PM Office related (From:Visual C# General)
    Thursday, May 17, 2012 8:58 AM

Answers

  • Hi, Anvin

    I have been trying to go through this scenario in C# console application. It turns out the Word Object model could not do exactly same as we insert header manually.  Below is the code sample which for your reference:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Word = Microsoft.Office.Interop.Word;
    namespace Console_AddHeader
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word.Application wordApp = new Word.Application();
                wordApp.Visible = true;
                try
                {
                    //The document to work on.
                    Word.Document doc = wordApp.Documents.Open(@"C:\workingdir\How to create a header of built in style.docx");
                    //Attaching template which contains built-in building block; headers are included in these building blocks
                    doc.set_AttachedTemplate(@"C:\Users\<your user name>\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx");
                    Word.Template template = wordApp.Templates[1];
                    #region Below code generated from Word Macro recording.
                    if (wordApp.ActiveWindow.View.SplitSpecial != Word.WdSpecialPane.wdPaneNone)
                        wordApp.ActiveWindow.Panes[2].Close();
                    else
                    {
                    }
                    if (wordApp.ActiveWindow.ActivePane.View.Type == Word.WdViewType.wdNormalView || wordApp.ActiveWindow.ActivePane.View.Type == Word.WdViewType.wdOutlineView)
                        wordApp.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintPreview;
                    else
                    {
                    }
                    #endregion
                    //Open header edit mode. Equvalent to double click header area
                    wordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
                    //Below building block is the header we want to insert.  However, the code doesn't insert the header like we add manually. 
                    //Not sure if this is product defect. To be confirmed.
                    //The auto added header contains Company field which is not expected;
                    //The auto added header has title field bad referenced. Alt+ F9 to see background field code, Update field parameter to "Title".
                    //The auto added header has page field, instead of date field. This is different as the header we added manually.
                    //Other alternative header style: 
                    //Annual - has two more field than Austere (Odd page);
                    Word.BuildingBlock buildblock = template.BuildingBlockEntries.Item("Austere (Odd Page)");
                    //Insert specified style header
                    buildblock.Insert(wordApp.Selection.Range, true);
                }
                catch (System.Runtime.InteropServices.COMException)
                {
                    // Log the exception.
                }
                finally
                {
                    wordApp.Quit();
                }
            }
        }
    }

    Hope this gets you started.

    Forrest Guo | MSDN Community Support | Feedback to manager

    Tuesday, May 22, 2012 7:21 AM
    Moderator

All replies

  • Setting document.CustomDocumentProperties.PageSetup.OddAndEvenPagesHeaderFooter to true is a start. I think there's something else....

    EDIT: Try doing that, and then with the argument wdPrimaryHeaderStory or wdEvenPagesHeaderStory get the range from the document.StoryRanges collection.


    • Edited by JosephFox Thursday, May 17, 2012 3:53 PM
    Thursday, May 17, 2012 3:38 PM
  • Hi, Anvin

    I have been trying to go through this scenario in C# console application. It turns out the Word Object model could not do exactly same as we insert header manually.  Below is the code sample which for your reference:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Word = Microsoft.Office.Interop.Word;
    namespace Console_AddHeader
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word.Application wordApp = new Word.Application();
                wordApp.Visible = true;
                try
                {
                    //The document to work on.
                    Word.Document doc = wordApp.Documents.Open(@"C:\workingdir\How to create a header of built in style.docx");
                    //Attaching template which contains built-in building block; headers are included in these building blocks
                    doc.set_AttachedTemplate(@"C:\Users\<your user name>\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx");
                    Word.Template template = wordApp.Templates[1];
                    #region Below code generated from Word Macro recording.
                    if (wordApp.ActiveWindow.View.SplitSpecial != Word.WdSpecialPane.wdPaneNone)
                        wordApp.ActiveWindow.Panes[2].Close();
                    else
                    {
                    }
                    if (wordApp.ActiveWindow.ActivePane.View.Type == Word.WdViewType.wdNormalView || wordApp.ActiveWindow.ActivePane.View.Type == Word.WdViewType.wdOutlineView)
                        wordApp.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintPreview;
                    else
                    {
                    }
                    #endregion
                    //Open header edit mode. Equvalent to double click header area
                    wordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
                    //Below building block is the header we want to insert.  However, the code doesn't insert the header like we add manually. 
                    //Not sure if this is product defect. To be confirmed.
                    //The auto added header contains Company field which is not expected;
                    //The auto added header has title field bad referenced. Alt+ F9 to see background field code, Update field parameter to "Title".
                    //The auto added header has page field, instead of date field. This is different as the header we added manually.
                    //Other alternative header style: 
                    //Annual - has two more field than Austere (Odd page);
                    Word.BuildingBlock buildblock = template.BuildingBlockEntries.Item("Austere (Odd Page)");
                    //Insert specified style header
                    buildblock.Insert(wordApp.Selection.Range, true);
                }
                catch (System.Runtime.InteropServices.COMException)
                {
                    // Log the exception.
                }
                finally
                {
                    wordApp.Quit();
                }
            }
        }
    }

    Hope this gets you started.

    Forrest Guo | MSDN Community Support | Feedback to manager

    Tuesday, May 22, 2012 7:21 AM
    Moderator