Is it possible to see DOCVARIABLE and Word.Field.Result.Text

I’m currently using office automation and would like to switch to a managed component. So far Aspose.Word seems to be the best choice, yet i’m having a little difficulty with seeing DOCVARIABLE fields and being able to retrieve the current value. Is such functionality avaliable in your Word component?

Hi,

Thanks for the good words about our component.

Sorry, Variable collection itself is not supported at the moment, but it is still possible to retrieve DOCVARIABLE fields and their values from the document. Use IDocumentVisitor to retrieve any objects from the document. This interface provides an universal way to extract different parts of the document when it is impossible to do via implemented methods.

See here for a basic example of how to use this interface:

https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/

However, I’ve written a code sample that obtains all the DOCVARIABLE fields and their values from the document and stores them in a SortedList so you can get values by specifying either field name or index. This is a small but working application that iterates all the DOCVARIABLE fields and outputs their names/values to the console; however, it is far from ideal and intended only to provide the example.

using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using Aspose.Word;

namespace DocVariablesApp
{
    ///
    /// Summary description for Class1.
    ///
    class Class1
    {
        ///
        /// The main entry point for the application.
        ///
        [STAThread]
        static void Main(string[] args)
        {
            Document doc = new Document(@"D:\Tests\test.doc");
            VariablesVisitor visitor = new VariablesVisitor();

            // Enumerate all document content to the visitor
            doc.Accept(visitor);

            // Get list of variables
            SortedList variables = visitor.Variables;

            foreach (DictionaryEntry entry in variables)
            {
                Console.WriteLine("{0}{1}", entry.Key.ToString().PadRight(30), entry.Value);
            }

            Console.ReadLine();
        }
    }

    class VariablesVisitor : IDocumentVisitor
    {
        bool isDocVariable = false;
        bool isResult = false;
        string name;
        string result;
        SortedList variables = new SortedList();

        public SortedList Variables
        {
            get
            {
                return variables;
            }
        }

        public void RunOfText(Font font, string text)
        {
            // Get name or value of a variable
            if (isDocVariable)
            {
                if (!isResult)
                    name = ExtractName(text);
                else
                    result = text;
            }
        }

        public void FieldStart(FieldType fieldType)
        {
            // Set the flag if the following field is a DocVariable field
            isDocVariable = fieldType == FieldType.FieldDocVariable;
            isResult = false;
        }

        public void FieldSeparator()
        {
            isResult = true;
        }

        public void FieldEnd()
        {
            variables.Add(name, result);
        }

        ///
        /// Use a regex to extract field name from field code
        ///
        ///
        ///
        private string ExtractName(string fieldCode)
        {
            string pattern = @"\s+DOCVARIABLE\s+(?\w+) .+";
            Match match = Regex.Match(fieldCode, pattern);
            return match.Result("${name}");
        }
        // Other IDocumentVisitor methods have been skipped!
    }
}

Thank you,

Dimitry many thanks for the code sample, i was able to make it work for retrieving all the DocVariables. I’m very impressed that you went so far out of your way to provide me with this. I wish every company was like this. Dimitry i just have one last question. How can i update the field results?

Thank you once again,

Peter Garbuz
Vendisoft LLC

We’re always happy to help you.

Unfortunately, updating the field results is useless because this doesn’t update the variables themselves and if a user later updates the fields in MS Word, the field results will be recalculated.