Updating Fields in M$ Word from VB

  • 15 years ago

    I am building a form in Word in which I am making parts variable by introducing fields.  The only way I can keep from getting an error in generating a field is to declare it and give it a value from the VB editor within Word.  What I would like to do is set these variables from an independant VB program and then save the new document to a variable location.

    I can open and save OK, but I run into problems setting the fields.  Once I set them, I can see that they are changed but they do not show up in the word document.  This may be difficult to read, so I will explain the output from the below code.

    I have a test document with one field called "testvar".  If I run the below code, I first get a box with "Converted!" in it.  This shows that the value for field 'testvar' did get changed.  Then I see a box with "testvar", showing that the variable is named correctly and my syntax is ok in referencing it.  The last box comes up with "testvar" as well, showing that it is the first variable and an alternate way of referencing.  Here is where it goes wrong, at the last step the text box is filled with the original value set when I declared the field in Word itself.

    So is there a update fields type of command?  I know within Word I have to hit F9 to update fields.  Thanks for any help.

    Private Sub cmdReadClick()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        Dim nword As Long
        Dim newstring As String
       
        Set objWord = New Word.Application
        Set objDoc = New Word.Document
        Set objDoc = objWord.Documents.Open("F:\brian\test.doc")
        objDoc.Variables("test
    var").Value = "Converted!"
        MsgBox (objDoc.Variables("testvar").Value)
        MsgBox (objDoc.Variables("test
    var").Name)
        MsgBox (objDoc.Variables(1).Name)
       
        For nword = 1 To objDoc.Words.Count
            newstring = newstring & objDoc.Words(nword).Text
            txtDisplay.Text = newstring
        Next nword
       
        objDoc.Close False
        objWord.Quit False
    End Sub






























  • 15 years ago

    I did some digging last night, and found a update command.  It is nested in StoryRanges => Fields => Update.  Adjusted code is below in case anyone else wishes to do the same.

    Private Sub cmdReadClick()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        Dim nword As Long
        Dim newstring As String
        Dim aStory As Range
        Dim aField As Field
       
        Set objWord = New Word.Application
        Set objDoc = New Word.Document
        Set objDoc = objWord.Documents.Open("F:\brian\test.doc")
        objDoc.Variables("test
    var").Value = "Converted!"
       
        For Each aStory In objDoc.StoryRanges
            For Each aField In aStory.Fields
                aField.Update
            Next aField
        Next aStory
       
        For nword = 1 To objDoc.Words.Count
            newstring = newstring & objDoc.Words(nword).Text
            txtDisplay.Text = newstring
        Next nword
       
        objDoc.Close False
        objWord.Quit False
    End Sub





























Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC. As potential programmers, they are mentally mutilated beyond hope of regeneration.” - E. W. Dijkstra