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
DescriptionThis example shows how to read, write, and delete variables in a Word document in Visual Basic 6.
KeywordsWord, Microsoft Word, Office, Microsoft Office, variable
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.

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 cmdOpen_Click()
Dim doc_story As Word.Range
Dim word_var As Word.Variable
Dim txt As String

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

    ' Open the Document.
    Set m_WordDoc = m_WordApp.Documents.Open(txtFile.Text)

    ' Loop over all document stories.
    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.AddItem txt
    Next word_var

    cmdClose.Enabled = True
    fraNew.Enabled = True
End Sub

Private Sub cmdClose_Click()
    ' Save and close the document.
    m_WordDoc.Close True
    Set m_WordDoc = Nothing

    ' Clean up.
    m_WordApp.Quit
    Set m_WordApp = Nothing

    lstVariables.Clear
    cmdDelete.Enabled = False
    fraNew.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 cmdAdd_Click()
    m_WordDoc.Variables.Add txtNewName.Text, _
        txtNewValue.Text
    lstVariables.AddItem txtNewName.Text & " = " & _
        txtNewValue.Text
    txtNewName.Text = ""
    txtNewValue.Text = ""
End Sub

Private Sub cmdDelete_Click()
Dim var_name As String

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

    ' Remove the variable from the list.
    lstVariables.RemoveItem lstVariables.ListIndex

    cmdDelete.Enabled = lstVariables.ListIndex > -1
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