none
Document Variables in Word RRS feed

  • Question

  • where the document variables are located in a word document . How can we see the created documented variables in a word document.
    Wednesday, January 4, 2012 8:47 AM

All replies

  • Hi Shyamaldas

    You mean, how can the content of a document variable stored in the document be displayed to the user, as part of the document text?

    Insert a DocVariable field that references the name of the document variable.

    It's not clear to me whether you want code (and in what programming language) or the steps for the end-user?


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 4, 2012 9:10 AM
    Moderator
  • i have created code to insert a document variable but i dont know how to view the created document variable. i donot need the code ,i need the procedure for word 2007 to view the already created document variables.
    Wednesday, January 4, 2012 1:49 PM
  • Hi Shyamaldas

    Position the cursor where you want to see the content of the Document variable.

    Press Ctrl+F9 to insert a pair of { field braces }

    Within the braces type: DocVariable

    type a space

    Type the name of the document variable.

    Press F9 to update the DocVariable field. (This should also toggle the code view to the display view. Alt+F9 will always toggle the view of all fields in a document between the code and display views.)


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 4, 2012 2:27 PM
    Moderator
  • hi sir,

    i have created a document variable already but if i press Alt+F9 it is not showing that.. is there anyother way to view those document variables that has been already created

    Wednesday, January 4, 2012 5:55 PM
  • Hi Shyamaldas

    Creating a Document Variable in the document is one thing. Creating a DocVariable field in the document is something entirely different. Alt + F9 will not show the document variable, only field codes/field results.

    You must first insert a field code (using the steps I gave you), then update it (F9), then toggle it until you get the field results in place of the field codes (Alt+F9).

    By design, there is absolutely no option in the UI to edit the contents of document Variables and they can only be displayed using a DocVariable field.


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 4, 2012 8:11 PM
    Moderator
  •  public static void EmbedVariable(Word.Document doc, string msg)
                {
                  // Remove existing variables just in case.
                  foreach (Word.Variable v in doc.Variables)
                  {
                    if (v.Name.StartsWith(DOC_VARIABLE_PREFIX))
                    {
                      v.Delete();
                    }
                  }
                  // Maybe use System.IO.StringReader?
                  int count = ((msg.Length - 1) / PARTITION) + 1;
                  for (int i = 0; i < count; ++i)
                  {
                    object s = msg.Substring(
                        i * PARTITION,
                        i < count - 1 ? PARTITION : msg.Length - i * PARTITION);
                        //public string Substring(int startIndex,int length)
                        //substring(0,60000)

                    doc.Variables.Add(DOC_VARIABLE_PREFIX + i.ToString(), ref s);
                    //doc.Variables.Add(name,value);
                  }
                }

     

    sir this is the part of my name which inserts a document variables with the specified prefix given in DOC_VARIABLE_PREFIX variable.

    i stored a signature of a hashed value in that document variable . But i dont know how to see the stored value of that document variable.i need it for word 2007.

     

    in word 2003 i tried to insert a sample field which was already created by me using the option insert->field. and then i pressed Alt+F9 . It showed the content of the document variable.

    whether i should follow the same procedure for my above application also.? if so how should i view in word 2007

    thank you sir.

    Thursday, January 5, 2012 6:05 AM
  • Hi Shyamaldas

    <<in word 2003 i tried to insert a sample field which was already created by me using the option insert->field. and then i pressed Alt+F9 . It showed the content of the document variable.

    whether i should follow the same procedure for my above application also.? if so how should i view in word 2007>>

    Yes, the same procedure in 2007 and for which I gave you the steps I use.

    But if you need a dialog box: Insert/Text/Quick Parts/Fields


    Cindy Meister, VSTO/Word MVP
    Friday, January 6, 2012 8:09 AM
    Moderator
  • hi cindy,

    but that fields are not showing the document variable i created when i try to insert it.

    Friday, January 6, 2012 1:22 PM
  • Did you pass a string value to the Document Variable? A document variable must contain a string with at least 1 character in it.

    You also need to be sure you type in the correct name of the document variable. If you type it incorrectly you won't see anything.

    Perhaps you should open the document with some code an loop the Document.Variables collection to see what document variables it contains and what their names are.


    Cindy Meister, VSTO/Word MVP
    Friday, January 6, 2012 1:59 PM
    Moderator
  • Hi Shyamaldas

    There were some problems with Word 2007 and Document Variables, caused by some updates. That may also be part of the problem you're seeing. See for example

    http://www.eggheadcafe.com/microsoft/Word-VBA/35870079/docvariable-wont-work.aspx
    http://blogs.oracle.com/UPK/entry/tutor_caution_to_word_doc
    http://support.microsoft.com/kb/970226


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Monday, January 23, 2012 9:45 AM
    Friday, January 6, 2012 2:08 PM
    Moderator