Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRead, write, and delete document variables in a Word document in VB .NET
DescriptionThis example shows how to read, write, and delete document variables in a Word document in VB .NET
KeywordsWord, Microsoft Word, Office, Microsoft Office, variable, VB .NET
CategoriesOffice, Miscellany
 
This program requires a reference to the Word object model. Select Project/Add Reference. Click the COM tab and select "Microsoft Word 11.0 Object Library" (or whatever version you have on your system).

When you click the Open button, the program opens the Word document. It then loops through the document's Variables collection adding each variable's name and value to a ListBox.

A slightly odd thing happens in this code. The call to Open takes a parameter declared as an Object. If you try to pass in txtFile.Text, Visual Basic complains that it cannot convert an Object into a String. But wait. You're trying to do the opposite: pass a String in as an Object variable. That should work because a String is a type of Object (it inherits from Object).

The trick is that this parameter is declared without the ByVal keyword. That means it is passed by reference and the Open routine might change the Object's value. It cannot do that if you pass it a String instead of an Object so it complains. A bit backwards but it makes some sense.

When you click the Close button, the program closes the Word document.

 
Private m_WordApp As Word.Application
Private m_WordDoc As Word.Document

Private Sub btnOpen_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnOpen.Click
    Dim word_var As Word.Variable
    Dim txt As String

    ' Make a Word server object.
    m_WordApp = New Word.Application
    m_WordApp.Visible = False

    ' Open the Document.
    m_WordDoc = _
        m_WordApp.Documents.Open(DirectCast(txtFile.Text, _
        Object))

    ' Loop over all document stories.
    lstVariables.Items.Clear()
    For Each word_var In m_WordDoc.Variables
        txt = word_var.Name & " = "
        On Error Resume Next
        txt = txt & word_var.Value
        If Err.Number <> 0 Then txt = txt & "?????"
        On Error GoTo 0

        lstVariables.Items.Add(txt)
    Next word_var

    btnClose.Enabled = True
    grpNew.Enabled = True
End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnClose.Click
    ' Save and close the document.
    m_WordDoc.Close(True)
    m_WordDoc = Nothing

    ' Clean up.
    m_WordApp.Quit()
    m_WordApp = Nothing

    lstVariables.Items.Clear()
    btnDelete.Enabled = False
    grpNew.Enabled = False
End Sub
 
If you enter a new name and value and click the Add button, the program adds the new variable to the document's Variables collection.

If you select a variable in the LstBox and click Delete, the program gets the variable's name and uses it to remove the variable from the document's Variables collection.

 
Private Sub btnAdd_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnAdd.Click
    m_WordDoc.Variables.Add(txtNewName.Text, _
        DirectCast(txtNewValue.Text, Object))
    lstVariables.Items.Add(txtNewName.Text & " = " & _
        txtNewValue.Text)
    txtNewName.Text = ""
    txtNewValue.Text = ""
End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnDelete.Click
    Dim var_name As String

    ' Remove the variable from the document.
    var_name = Split(lstVariables.Text, " = ")(0)
    m_WordDoc.Variables.Item(DirectCast(var_name, _
        Object)).Delete()

    ' Remove the variable from the list.
    lstVariables.Items.RemoveAt(lstVariables.SelectedIndex)

    btnDelete.Enabled = Not (lstVariables.SelectedItem Is _
        Nothing)
End Sub
 
For more information on programming Word and other Microsoft Office applications with VBA, see my book Microsoft Office Programming: A Guide for Experienced Developers.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated