c# - Find Content Control by TagName in Word document in OpenXml -


i attempting insert text in content control in word document template using openxml. first search content control tag name , adding paragraph element in sdtblock below,

sdtblock contentblock = worddoc.maindocumentpart.document.body.descendants<sdtblock>()    .where(r => r.sdtproperties.getfirstchild<tag>().val == "assessmentsection")    .single(); 

but when execute statement, getting "object reference not set instance of object." error message. template document has content control , able find control using same above statement tagname different. after adding "assessmentsection" content control in template , while running program, "object reference..." error "assessmentsection" control , program fails. sure new content control tag name , title unique other content control.

can please me why strange behaviour occurring , how fix it???

you loop documents contentcontrols items , check tags, like:

foreach (word.contentcontrol contentcontrol in worddoc.contentcontrols) {    if (contentcontrol.tag != null)    {           ...    } } 

Comments