Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jun 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can I update variables (VBA (Word 2000))

    Can anyone tell me how I can get variables in a userform (attached to a template) to allow updates and edits? I have created variables to remember text from the userform textboxes so that the new document remembers what was in the textboxes - now I need to be able to edit these textboxes (in the document), hopefully without changing the variable names.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Can I update variables (VBA (Word 2000))

    You can change the text that appears in a textbox FormField without changing its name. You also can store variables invisibly as Document Variables to store prior entries in a UserForm; you can change the value of Document Variables without changing their names.

    Can you further clarify or post an example of what you mean? I'm not sure which is your situation, or maybe it's neither.

  3. #3
    New Lounger
    Join Date
    Jun 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can I update variables (VBA (Word 2000))

    I think I need to change the value of the Document variables without changing the names. I have a userform in a template. I set the template textboxes with default values. When a new document is created using that template I am able to change the textbox values without changing the default values in the template. I have created variables to store prior entries in a UserForm so that the next time I open the document I can see the values I have edited - rather than the default template values. What I need to be able to do now is change these edited textbox values in the document. At the moment once I have saved the textbox value and created the variable to store the information I am unable to go back and edit this textbox value as it tells me that variable name already exists. I hope that is a little clearer.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Can I update variables (VBA (Word 2000))

    Instead of using .Add again, you need to set the .Value property. Here's an example:
    <pre>Option Explicit

    Sub UpdateMyDocVars()
    ' Test with a known DocVar or you'll crash before you get to the function!
    With ActiveDocument
    Debug.Print .Variables("MyText1").Value
    If DocVarUpdate("MyText1") Then
    Debug.Print "--> changed to " & .Variables("MyText1").Value
    Else
    Debug.Print "--> not updated!"
    End If
    End With
    End Sub

    Function DocVarUpdate(strVarName As String) As Boolean
    Dim dVar As Word.Variable
    ' Look for the named variable in the document and, if
    ' found, update it, then exit this function
    For Each dVar In ActiveDocument.Variables
    If dVar.Name = strVarName Then
    dVar.Value = dVar.Value & "-NewPart"
    Set dVar = Nothing
    DocVarUpdate = True
    Exit Function
    End If
    Next dVar
    ' Variable was not found, return False from the function
    Set dVar = Nothing
    DocVarUpdate = False
    End Function</pre>

    You'll need to replace the bold part with your own code to assign the UserForm fields to the document variable. Does this help point the way?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •