Update Word form controls in C# and VB.NET

The following example shows how you can write or modify values of an existing text, checkbox, and drop-down form fields in a Word document using the GemBox.Document component.

using GemBox.Document;
using System;

// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

var document = DocumentModel.Load("FormFilled.docx");

// Get a snapshot of all form fields in the document.
var formData = document.Content.FormFieldsData;

// Update "FullName" text field.
var fullNameData = (FormTextData)formData["FullName"];
fullNameData.Value = "Jane Doe";

// Update "BirthDate" text field.
var birthDateData = (FormTextData)formData["BirthDate"];
birthDateData.Value = new DateTime(2000, 2, 29);

// Check "Married" check-box field.
var marriedData = (FormCheckBoxData)formData["Married"];
marriedData.Value = true;

// Select "Female" from drop-down field.
var genderData = (FormDropDownData)formData["Gender"];
genderData.SelectedItemIndex = genderData.Items.IndexOf("Female");

document.Save("Form Updated.docx");
Imports GemBox.Document
Imports System

Module Program

Sub Main()

' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")

Dim document = DocumentModel.Load("FormFilled.docx")

' Get a snapshot of all form fields in the document.
Dim formData = document.Content.FormFieldsData

' Update "FullName" text field.
Dim fullNameData = DirectCast(formData("FullName"), FormTextData)
fullNameData.Value = "Jane Doe"

' Update "BirthDate" text field.
Dim birthDateData = DirectCast(formData("BirthDate"), FormTextData)
birthDateData.Value = New DateTime(2000, 2, 29)

' Check "Married" check-box field.
Dim marriedData = DirectCast(formData("Married"), FormCheckBoxData)
marriedData.Value = True

' Select "Female" from drop-down field.
Dim genderData = DirectCast(formData("Gender"), FormDropDownData)
genderData.SelectedItemIndex = genderData.Items.IndexOf("Female")

document.Save("Form Updated.docx")

End Sub
End Module
Updating a Word document's text, checkbox and drop-down form fields in C# and VB.NET
Screenshot of a Word file with updated form field values

When setting the FormText field's value to an IFormattable type (like a number or a date), the field's resulting text will depend on the value of FormTextData.ValueFormat.

Next steps

GemBox.Document is a .NET library that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?