Results 1 to 9 of 9
  1. #1
    Lounger
    Join Date
    Dec 2011
    Posts
    33
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Help with DocVariable value

    Hi folks,

    I've inherited a document written several years ago and I need to add some more tables and figures. Whoever wrote the original document did a complicated table/figure numbering, in which they included a DocVariable as part of each table figure number. I now have a problem: when I update the fields in the document (which I'll have to do), the DocVariable's value is completely wrong. It should be "2.7.2-" but instead it's displaying some crazy text.

    Screen shot "Table field 1" shows it as I want it, before I update. "Toggle field" shows the field code with the DocVariable displayed, and "Updated field" shows it when I update or add new tables.

    Bottom line: how do I update the value of that DocVariable? I've looked in Document Properties and it's not there. I tried updating the value via VBA. If I add a new DocVariable called "TablePrefix" I get an error saying it already exists; if I try to update the value via VBA I get an error saying the variable's not defined.

    Thanks in advance.

    Beej
    Attached Images Attached Images

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    OK, let's start at the beginning, by finding out what document variables the document contains:
    Code:
    Sub Test()
    Dim DocVar As Variable, StrOut As String
    For Each DocVar In ActiveDocument.Variables
      StrOut = DocVar.Name & ": " & DocVar.Value & vbCr
    Next
    MsgBox StrOut
    End Sub
    What is returned for your problem variable?
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts
    Paul knows far more about document variables than do I. Follow his suggestion. If you need help with using it, see Installing Macros.

    A helpful, although complex, tool for dealing with document variables can be downloaded free from here. It deals with much more than variables but can help you in identifying and changing them without writing your own vba.

    Document variables are often used because they are not easily accessed by users.
    Charles Kyle Kenyon
    Madison, Wisconsin

  4. #4
    Lounger
    Join Date
    Dec 2011
    Posts
    33
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Hi Paul,

    Thank you as always. You are truly an invaluable resource.

    Here's the screen shot of the message displayed. Variable name and value.JPG

    Beej

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Does this strange update behaviour happen even if you update the field without adding a new table? Your pics suggest that either the document has acquired some form of corruption or that someone's had a go at modifying its XML code using the Microsoft Windows SDK. See: https://msdn.microsoft.com/en-us/lib...or=-2147217396
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #6
    Lounger
    Join Date
    Dec 2011
    Posts
    33
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Yes. Any update to the table or figure caption fields produces this same result.

  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    In that case, the safest course is to assume there's been some document corruption. Corrupt documents can often be 'repaired' by inserting a new, empty, paragraph at the very end, copying everything except that new paragraph to a new document based on the same template (headers & footers may need to be copied separately), closing the old document and saving the new one over it. You'll also need to recreate the document variables. That can be done with a macro like:
    Code:
    Sub AddEditDocumentVariable()
    Application.ScreenUpdating = False
    Dim varNm As String, varVal As String, i  As Long, bFnd As Boolean
    varNm = InputBox("What is the name of the variable to add?", "Variable Name")
    If Trim(varNm) = "" Then Exit Sub
    varVal = InputBox("What is the value of the " & varNm & "variable?", "Variable Value")
    With ActiveDocument
      bFnd = False
      For i = 1 To .Variables.Count
        If .Variables(i).Name = varNm Then
           .Variables(i).Value = varVal
          bFnd = True
          Exit For
        End If
      Next
      If bFnd = False Then .Variables.Add varNm, varVal
    End With
    Application.ScreenUpdating = True
    End Sub
    The above macro lets you both add new variables and update existing ones.

    For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
    For Mac macro installation & usage instructions, see: http://word.mvps.org/Mac/InstallMacro.html
    Last edited by macropod; 2016-02-24 at 18:03.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  8. The Following 2 Users Say Thank You to macropod For This Useful Post:

    Charles Kenyon (2016-02-25),ItsjustBeej (2016-02-24)

  9. #8
    Lounger
    Join Date
    Dec 2011
    Posts
    33
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Again, thank you. Yes, I'm very familiar with the copy-everything-but-the-final-paragraph trick.

  10. #9
    Lounger
    Join Date
    Dec 2011
    Posts
    33
    Thanks
    16
    Thanked 0 Times in 0 Posts
    It worked. Copied everything except the final para, pasted into new document, and ran some code to add the variable & then assign the correct value. Voila!

    THANK YOU. You've me look very good in the eyes of my boss.

    Beej

Posting Permissions

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