c# - Create Word File Using Microsoft.office.Interop.Word -


i using code create word file programmatically using microsoft.interop.word c#, when create , have aproblem repeat header of first page of pages of documents, how can set put header first page of document only?

and that's code :

 public void create_document()     {         try         {             //create instance word app             microsoft.office.interop.word.application winword = new microsoft.office.interop.word.application();              //set animation status word application             winword.showanimation = false;              //set status word application visible or not.             winword.visible = false;              //create missing variable missing value             object missing = system.reflection.missing.value;              //create new document             microsoft.office.interop.word.document document = winword.documents.add(ref missing, ref missing, ref missing, ref missing);              //add header document             foreach (microsoft.office.interop.word.section section in document.sections)             {                 //get header range , add header details.                 microsoft.office.interop.word.range headerrange = section.headers[microsoft.office.interop.word.wdheaderfooterindex.wdheaderfooterprimary].range;                 headerrange.fields.add(headerrange, microsoft.office.interop.word.wdfieldtype.wdfieldpage);                 headerrange.paragraphformat.alignment = microsoft.office.interop.word.wdparagraphalignment.wdalignparagraphcenter;                 headerrange.font.colorindex = microsoft.office.interop.word.wdcolorindex.wdblue;                 headerrange.font.size = 30;                 headerrange.text = "exam dates " + course_name;                 section.borders.enable = 1;                 section.borders.outsidelinestyle = wdlinestyle.wdlinestylesingle;                 section.borders.outsidelinewidth = wdlinewidth.wdlinewidth300pt;                 section.borders.outsidecolor = wdcolor.wdcolorblack;              }                //adding text document             document.content.setrange(0, 0);             // document.content.text = "exam dates :" + environment.newline;              //add paragraph heading 1 style             microsoft.office.interop.word.paragraph para1 = document.content.paragraphs.add(ref missing);             object styleheading1 = "heading 1";             para1.range.set_style(ref styleheading1);             para1.range.text = "exam dates";             para1.range.insertparagraphafter();                //create 5x5 table , insert dummy record             // microsoft.office.interop.word.table firsttable = document.tables.add(para1.range, 4,2, ref missing, ref missing);             microsoft.office.interop.word.table firsttable = document.tables.add(para1.range, 1, 2, ref missing, ref missing);              firsttable.borders.enable = 1;              firsttable.cell(1, 1).range.text = "exam name";             firsttable.cell(1, 2).range.text = "exam date";             connection.close();             connection.open();             sqlcommand cmd = new sqlcommand("select examname,date exam coursenum='" + course_number + "'", connection);             sqldatareader reader = cmd.executereader();              int introw = 2;              // retrieve data , insert new rows.             object beforerow = type.missing;             string[] dates;             while (reader.read())             {                 dates = reader[1].tostring().split(' ');                 firsttable.rows.add(ref beforerow);                 firsttable.cell(introw, 1).range.text = reader[0].tostring();                 firsttable.cell(introw, 2).range.text = dates[0];                  introw += 1;             }             //save document             object filename = environment.getfolderpath(environment.specialfolder.desktop) + "\\attachments\\exams.docx";            document.saveas2(ref filename);             // document.save();             ((microsoft.office.interop.word._document)document).close(ref missing, ref missing, ref missing);             ((microsoft.office.interop.word._application)winword).quit(ref missing, ref missing, ref missing);  }         catch (exception ex)         {             ex.tostring();         }           }//create document 

i believe looking other wdheaderfooterindex property wdheaderfooterfirstpage 1 looking for. @arun’s answer @ different first page in document using microsoft office interop word in c#... see need set documents pagesetup.differentfirstpageheaderfooter value -1.

simply add first line below code , change second line in code header range first page only.

document.pagesetup.differentfirstpageheaderfooter = -1;  // <-- add line microsoft.office.interop.word.range headerrange = section.headers[microsoft.office.interop.word.wdheaderfooterindex.wdheaderfooterfirstpage].range; //microsoft.office.interop.word.range headerrange = section.headers[microsoft.office.interop.word.wdheaderfooterindex.wdheaderfooterprimary].range; 

this should header appear on first page of each section. hope helps.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -