c# Editing word file ( setting choice-fields)

291 Views Asked by At

i`m using function below to fill properly data in doc.file by bookmarks and it works good.

public void findAndReplace(Word.Document doc, object bookmark, object replaceWith)
        {
            Word.Range rng = doc.Bookmarks.get_Item(ref bookmark).Range;
            rng.Text = replaceWith.ToString();
            object oRng = rng;
            doc.Bookmarks.Add(bookmark.ToString(), ref oRng);
        } 

I`ve got problem with setting values of choice-fields in word file. My questions is, is it even possible to set this kind of data from my c# application ? There is any method to generate Selected or unselected fields like for example in point 21 in link below or only set its value ? http://www.fotosik.pl/pokaz_obrazek/c2409113d79afeba.html

And last question is it possible and reasonable to generate whole report from doc file ?
I`m looking for some solution which helps to generate completely my declaration by c#.

1

There are 1 best solutions below

1
On

Yes that is possible by programmatically adding Content Controls.

This is how you would add a Check Box Content Control:

this.Paragraphs[1].Range.InsertParagraphBefore();
this.Paragraphs[1].Range.Select();

Microsoft.Office.Tools.Word.ContentControl checkBoxControl1 = 
     this.Controls.AddContentControl("checkBoxControl1", Word.WdContentControlType.wdContentControlCheckBox);

checkBoxControl1.Checked = true;

You can generate entire documents from c#. I would suggest this tutorial from MSDN about creating templates by using content controls.