This is my code.
using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document)) {
MainDocumentPart mainPart = package.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = new Body();
SdtBlock sdtBlock1 = new SdtBlock();
SdtProperties sdtProperties1 = new SdtProperties();
SdtAlias sdtAlias1 = new SdtAlias() { Val = "select" };
Tag tag1 = new Tag() { Val = "select" };
SdtId sdtId1 = new SdtId() { Val = 364947247 };
SdtContentComboBox sdtContentComboBox1 = new SdtContentComboBox();
ListItem listItem1 = new ListItem() { Value = "Choose an item." };
ListItem listItem2 = new ListItem() { DisplayText = "Rama", Value = "Rama" };
sdtContentComboBox1.Append(listItem1);
sdtContentComboBox1.Append(listItem2);
sdtProperties1.Append(sdtAlias1);
sdtProperties1.Append(sdtContentComboBox1);
SdtContentBlock sdtContentBlock1 = new SdtContentBlock();
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Rama";
run1.Append(text1);
sdtBlock1.Append(sdtProperties1);
sdtBlock1.Append(sdtContentBlock1);
mainPart.Document.Body.Append(sdtBlock1);
}
I am trying to create a document and add a Dropdownlist content control to it but when I run this code VS Debugger is stops the program saying "Null Reference Exception was unhandled". I want to know How to append or insert a new content control on a newly created document.
Thanks in advance.
