How do I modify the value of a checkbox in a word document using C

R

Ray

Hi all,
I am a bit of a newbie at C# and am trying to set up a document that I will
be modifying based on values from a database. I am using a word document,
because it is the "official" reporting form required by my company. I can
open the document, and write to bookmarks that I am setting in each table
field, but I need to know how to manipulate a checkbox. Can anyone help me?
I have edited this post because I have added some other code. The document
file I am using is a document that has everything inside a table. I have
bookmarked all the text locations, and have bookmarked the table cell with
one of the checkboxes if that helps. Anyway, below is my code. I just need
to know how to tell my program to expect a checkbox at that location, and
modify it based on the alue of a checkbox in my application.

Thanks in advance for all the help.

Ed


private void CreateWordFile()
{
DateTime myDate;
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; //\endofdoc is a predifined
bookmark.

//Start Word and open template.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"C:\Plant Tracker\AMP-108_old.doc";
oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing);

//Edit Bookmarks in File.
object oBookMark = "FacNameBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = "Magnolia
Energy";
oBookMark = "EventDateBkMk";
myDate = Convert.ToDateTime(eventDateTxtBx.Text);
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
myDate.ToShortDateString().ToString();
oBookMark = "EventNumBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
eventNoTxtBx.Text;
oBookMark = "TitleBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
titleTxtBx.Text;
oBookMark = "SummaryBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
summaryTxtBx.Text;
oBookMark = "DetailBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
detailTxtBx.Text;
oBookMark = "RespBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
caRespTxtBx.Text;
oBookMark = "CADueBkMk";
myDate = Convert.ToDateTime(caDueDateTxtBx.Text);
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
myDate.ToShortDateString().ToString();
oBookMark = "CorrActBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
corrActionTxtBx.Text;
oBookMark = "PrepByBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
prepByTxtBx.Text;
oBookMark = "PrepDateBkMk";
myDate = Convert.ToDateTime(prepDateTxtBx.Text);
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
myDate.ToShortDateString().ToString();
oBookMark = "PMAppBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
pmApprovTxtBx.Text;
oBookMark = "PMDateBkMk";
myDate = Convert.ToDateTime(pmDateTxtBx.Text);
oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text =
myDate.ToShortDateString().ToString();

oBookMark = "FinakChkBkMk";
//How do I access the checkbox that is stored in the table cell
at this bookmark?

// Save as new file with file name of "Incident" + EventNumber +
Date.
myDate = Convert.ToDateTime(eventDateTxtBx.Text);
object saveFileName = @"C:\Plant Tracker\Incident_Report_" +
eventNoTxtBx.Text + "_" + myDate.ToShortDateString().ToString().Replace('/',
'-');
MessageBox.Show(saveFileName.ToString());
oDoc.SaveAs(ref saveFileName, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing);
 
J

Jay Freedman

oBookMark = "FinakChkBkMk";
//How do I access the checkbox that is stored in the table
cell at this bookmark?

It isn't necessary to add a bookmark to the cell containing the checkbox.
When you inserted the checkbox in the document, it was automatically
assigned a name, which is both an ordinary bookmark associated with the
checkbox and the checkbox's name as a FormField object. If you open the
document manually, unprotect it (if it's protected), and double-click the
checkbox, you'll see the name in the Bookmark box of the Properties
dialog -- and you can change it from the default Check1 to something
meaningful such as "FinalChk". (You did mean Final and not Finak, right?)

In your code, you can address the checkbox directly like this:

object oFormField = "FinalChk";
oDoc.FormFields.get_Item(ref oFormField).CheckBox.Value = True;

or, if you'd rather keep the bookmark you added to the cell, you can go
indirectly through the Bookmarks collection like this:

oBookMark = "FinalChkBkMk";
oDoc.Bookmarks.get_Item(ref oBookMark).Range.FormFields(1).CheckBox.Value
= True;

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top