Imports Docs.Word


Structure Product
	Friend ProductName As String
	Friend CategoryIndex As Integer
	Friend IsInStock As Boolean
End Structure


Class FormFields_Fill
	Friend Shared Function Main1() As Document
		'	It's an example of some database, you can use any database you need
		Dim Products As Product() = { _
		New Product() With {.ProductName = "Forrest Gump", .CategoryIndex = 0, .IsInStock = True}, _
		New Product() With {.ProductName = "Harry Potter", .CategoryIndex = 1, .IsInStock = False}, _
		New Product() With {.ProductName = "Call of Duty", .CategoryIndex = 2, .IsInStock = True}, _
		New Product() With {.ProductName = "Titanic", .CategoryIndex = 0, .IsInStock = True}, _
		New Product() With {.ProductName = "World Of Warcraft", .CategoryIndex = 2, .IsInStock = False}}

		'	Open the document with given name and get NodeList of first section
		Dim Doc As Document = Document.ReadDoc(Form1.SourceFolder + "FormFields - Fill.doc")
		Dim Nodes As NodeList = Doc.Sections(0).Nodes

		'	For all products in our database...
		For i As Integer = 0 To 4
			'	Get next paragraph
			Dim Par As Paragraph = DirectCast(Nodes(i + 1), Paragraph)

			'	Search all textruns of this paragraph
			For j As Integer = 0 To Par.Textruns.Count - 1
				'	If this textrun is of FormField type...
				If Par.Textruns(j).Type = TextrunType.FormField Then
					Dim FF As FormField = Par.Textruns(j).Fields.FormField
					FF.Textruns.Clear()

					'	If it's textbox - insert Products[i].ProductName in it
                    If FF.Type = FormFieldType.Textbox Then
                        FF.Textruns.Add(New Textrun(Products(i).ProductName))
                    End If

					'	If it's a droplist
					'	Change its current selected item to value of Products[i].CategoryIndex
                    If FF.Type = FormFieldType.DropDown Then
                        FF.DropDown.CurrentItemIndex = Products(i).CategoryIndex
                    End If

					'	If it's a checkbox
					'	Change its current state to value of Products[i].IsInStock
                    If FF.Type = FormFieldType.Checkbox Then
                        FF.CheckBox.Checked = Products(i).IsInStock
                    End If
				End If
			Next
		Next

		Return Doc
	End Function
End Class
