Share via

Updating Docvariables

Anonymous
2011-07-28T14:36:51+00:00

I have a userform that populates various docvariables for inclusion in a Word document, however I would like the ability if possible to update certain fields after they have been populated in the document. For example if I had a docvariable in the document that had a value of 'blah' when it was populated by the userform, how could I go about modifing that value and other subsequent itterations of that value throughout the document (thinking in the case of a mistake when the user inputted the value)? So in effect I need to repopulate the document again in effect - just wondering if it is possible?

Can macros call functions from a user form?

Thinking out loud really and just wondering how I could go about it?

Regards

Jasper

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2011-08-13T01:19:20+00:00

If you get this set up correctly, the userform should be populated after closing and reopening the document. The reason is that document variables are saved in the document's file, and they're still defined when you reopen the document.

To your second question: Yes, you need to adapt the code to use the actual names of the document variables in your document. The code I showed was just an example, and it only tests the single document variable called "name" -- if you actually have one called "name".

For a real UserForm_Initialize() procedure, you could repeat the same kind of code for each of the variables, like

   If DocVarExists("address") Then

      docvarval = ActiveDocument.Variables("address").Value

   Else

      docvarval = ""

   End If

   If DocVarExists("phone") Then

      docvarval = ActiveDocument.Variables("phone").Value

   Else

      docvarval = ""

   End If

but that gets very tedious and hard to maintain. The next step is to write a function that takes the document variable's name as an argument and returns the value (or an empty string) to put into the userform's field:

Function GetDocVar(varName As String) As String

   Dim docvarval As String

   If DocVarExists(varName) Then

      docvarval = ActiveDocument.Variables(varName).Value

   Else

      docvarval = ""

   End If

   GetDocVar = docvarval

End Function

(This is in addition to the DocVarExists function you already have.) Then the Userform_Initialize procedure can just be a series of lines like this, one for each document variable / userform field combination in your document:

Private Sub UserForm_Initialize()

    txtAddress.Text = GetDocVar("address")

    txtPhone.Text = GetDocVar("phone")

    ' etc. ...

End Sub

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2011-07-29T23:47:50+00:00

I suppose you have a Document_New or AutoNew macro that displays the userform as the first thing the user sees when they create a new document from this template -- that's the usual arrangement. What you need to add is a button that runs another macro that shows the same userform again. The button can be on the Quick Access Toolbar, in a custom group on the ribbon, or a MacroButton field in the body of the document (http://www.word.mvps.org/FAQs/TblsFldsFms/UsingMacroButton.htm).

My code isn't doing any kind of validation or checking for unfilled values. If your code for checking the textboxes is part of the userform, then it will run every time the userform is shown, regardless of whether it's shown by the AutoNew macro or by any other macro.

Never be sorry about asking questions -- keep going until you understand exactly what you need!

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-08-01T11:50:51+00:00

    Jay

    Thanks for the reply and your patience, I did what you said and created a macro to run the AutoNew macro that initiates the form, it pops up with the values as entered and I can modify them accordingly.

    Once again thanks for your help

    Jasper

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-07-29T07:03:18+00:00

    Jay thanks for the detailed reply there, would this in effect work only when the presented userform is in effect ? My thinking was the user has submitted the form and then on looking at the document that has been generated has noticed that say one of the fields is wrong and tries to change it  when the form is actually closed. Is this the what you are suggesting above - sorry if I am getting confused?

    Just for my piece of mind I already have code in place to ensure that the user enters a value into each textbox is that your code is checking for also?

    Sorry if it sounds a bit noddy! - still learning

    Jasper

    Was this answer helpful?

    0 comments No comments
  3. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2011-07-28T17:02:09+00:00

    If the user made a mistake, the proper course is to show the userform again, populated with the current values of the docvariables, and let the user make corrections.

    In the userform's UserForm_Initialize() procedure, try to read the values of the docvariables and put those values into the userform controls. Note that if you try to get the .Value of a docvariable that doesn't exist, you get an Error 5825, "Object has been deleted". There are three ways to handle this: (1) Create all the docvariables in the template and assign a single space character to each one, so you know the variables will always exist;

    or (2) trap the error like

       On Error Resume Next

       docvarval = ActiveDocument.Variables("name").Value

       If Err.Number <> 0 Then

          docvarval = ""

       End If

    or (3) (my preference) write an auxiliary function to test whether the docvariable exists:

    Function DocVarExists(varName As String) As Boolean

        Dim docvar As Variable

        Dim ret As Boolean

        ret = False

        For Each docvar In ActiveDocument.Variables

            If LCase(docvar.Name) = LCase(varName) Then

                ret = True

                Exit For

            End If

        Next

        DocVarExists = ret

    End Function

    Then you can use it in the UserForm_Initialize() procedure this way:

       If DocVarExists("name") Then

          docvarval = ActiveDocument.Variables("name").Value

       Else

          docvarval = ""

       End If

    Was this answer helpful?

    0 comments No comments