I am trying to build a simple template engine that will do the following:
1) Open a word file that I have already inserted a series of bookmarks into.
2) Read through the file and count up the # of bookmarks.
3) Dynamically add to the main panel a series of labels and text boxes named after each bookmark found in the document.
4) Use the input from the text boxes to fill out the template (replace the bookmark text with the text from the text box.
I can open the file, read it, count the bookmarks, and get their names. I even understand how to replace the text, but I am having trouble creating the labels and boxes and adding them to the panel. Below is my code:
Code:
Public Class Form1
Private Sub btnFindFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindFile.Click
With OpenFileDialog1
.ShowDialog()
If Windows.Forms.DialogResult.OK Then
Dim theFileName As String = .FileName
Dim objWord As Word.Application
objWord = New Word.Application
objWord.Visible = True
Dim docToChange As Word.Document
docToChange = objWord.Documents.Open(theFileName)
Dim bookmarks As Word.Bookmarks = docToChange.Bookmarks
Dim countBookmarks As Integer = bookmarks.Count
Dim counter As Integer = 1
Dim textForDisplay As String
While counter <= countBookmarks
textForDisplay = docToChange.Bookmarks(counter).Name
counter = counter + 1
Dim newLabel As New Label
newLabel.Name = textForDisplay
newLabel.Visible = True
newLabel.Text = textForDisplay
Panel2.Controls.Add(newLabel)
End While
objWord.Quit()
End If
End With
End Sub
End Class
Thank you for all your help. - Adam