locked
Edit ms word header and footers with c#

    Question

  • Hi all, is there a way of editing the existing the headers and footers of word documents with c#? basically im looking to change the image in the header and footers of a large number of documents. I was wondering how i access the header section of a document?
    thanks

    oh and im using word 2003
    Friday, July 10, 2009 10:50 AM

Answers

  • I think you can use Microsoft Office Interop assemblies to achieve this, the following code will give you an idea.

    Microsoft.Office.Interop.Word.Application oWord;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = false; //to avoid displaying the Word Application
                object strDocName = @"d:\test.doc";
                object objBool = false;
                object objNull = System.Reflection.Missing.Value;
                Document oMyDoc = oWord.Documents.Open(ref strDocName, ref objBool, ref objBool, ref objBool, ref objNull,
                    ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);

                object objReference = System.Reflection.Missing.Value;
                object objFoot = "this is footer";
               
                object objStart = 0;
                object objEnd = oMyDoc.Words.Count;

                if (oMyDoc.Sections.Count > 0)
                {
                    oMyDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "My Header";
                    oMyDoc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "My Footer";
                }
    • Proposed as answer by CubePS Saturday, July 11, 2009 3:14 AM
    • Marked as answer by Harry Zhu Friday, July 17, 2009 8:02 AM
    Saturday, July 11, 2009 2:34 AM

All replies

  • Hi,
    It is possible to change header & footer for word 2007 documents using C# and Open XML.

    -Ram
    Friday, July 10, 2009 11:58 AM
  • how would i do it with word 2003 documents though? because they arent xml as far as im aware
    Friday, July 10, 2009 12:18 PM
  • I think you can use Microsoft Office Interop assemblies to achieve this, the following code will give you an idea.

    Microsoft.Office.Interop.Word.Application oWord;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = false; //to avoid displaying the Word Application
                object strDocName = @"d:\test.doc";
                object objBool = false;
                object objNull = System.Reflection.Missing.Value;
                Document oMyDoc = oWord.Documents.Open(ref strDocName, ref objBool, ref objBool, ref objBool, ref objNull,
                    ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);

                object objReference = System.Reflection.Missing.Value;
                object objFoot = "this is footer";
               
                object objStart = 0;
                object objEnd = oMyDoc.Words.Count;

                if (oMyDoc.Sections.Count > 0)
                {
                    oMyDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "My Header";
                    oMyDoc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "My Footer";
                }
    • Proposed as answer by CubePS Saturday, July 11, 2009 3:14 AM
    • Marked as answer by Harry Zhu Friday, July 17, 2009 8:02 AM
    Saturday, July 11, 2009 2:34 AM
  • Hi,

    You might have got more informatin from the thread:
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/1ed6f584-97b9-4ecc-b021-9ab17bf7029b

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, July 17, 2009 8:02 AM