We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert Combo Box 2007 in word document

Hi,

How to insert with Aspose.Words a combo Box 2007 in documents programmatically or find an existing combo Box to add items on it.

I could not find the formfields on the document Word 2007 using doc.Range.FormFields

Thank you

Kamel

Hi Kamel,


Thanks for your inquiry. Placing form fields into the document via Aspose.Words is easy, Document Builder have special methods for inserting them. Please follow up the links to Insert / Extract form fields:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/form-fields-in-aspose-words.html


Code snippet:

// Create document and DocumentBuilder<o:p></o:p>

Document doc = new Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);<o:p></o:p>

<o:p> </o:p>

// Insert few form fields in to the docuemnt<o:p></o:p>

// insert text input<o:p></o:p>

builder.Write(“Name:\t”);<o:p></o:p>

builder.InsertTextInput(“name”, TextFormFieldType.Regular, “”, " ", 0);<o:p></o:p>

builder.Writeln();<o:p></o:p>

<o:p> </o:p>

// Insert combo box<o:p></o:p>

builder.Write(“Sex:\t”);<o:p></o:p>

builder.InsertComboBox(“sex”, new string[] { “male”, “female” }, 0);<o:p></o:p>

builder.Writeln();


Hi Imran,

I'm looking for combo Box 2007, please open the attached document and try to insert a combo box like one of the already inserted manually combo box in the document or try to find them by using doc.Range.FormFields.

Thank you

Kamel

Hi
Kamel,


Thanks for your inquiry. Actually the combo box in your Word document is represented by a Structured Document Tag (SDT). You can insert such SDT nodes in Word document by using the following code snippet:

Document
doc = new Document();

StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.ComboBox, MarkupLevel.Block);

sdt.ListItems.Add(new SdtListItem(“Choose an item”, “-1”));
sdt.ListItems.Add(new SdtListItem(“Item 1”, “1”));
sdt.ListItems.Add(new SdtListItem(“Item 2”, “2”));

doc.FirstSection.Body.AppendChild(sdt);

doc.Save(@“c:\temp\out.docx”);

Moreover, I would suggest you please visit the following article for more details about SDT:
http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.markup.structureddocumenttag.html

I hope, this will help.

Best Regards,

Hi Kamel,


Thanks for your inquiry. Please follow up the code snippet to insert or extract combo box.

Document doc = new Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertComboBox("FirstName", new string[] { "Alex", "Den", "Ben" }, 0);

doc.Protect(ProtectionType.AllowOnlyFormFields);

doc.Save("d:/temp/comboout.doc");


Extract combo box as following:


Document doc = new Document("d:/temp/comboout.docx");

FormField field = doc.Range.FormFields[0];



Note: Attached Word document don't have form fields, anyhow there is a StructuredDocumentTag.


Document doc = new Document("d:/temp/InsertComboBox2007.docx");

StructuredDocumentTag tag = StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);

// Get the text of the content control

string tagText = tag.ToTxt();


Hope this will help. In case of any ambiguity, please let mw know.