简体   繁体   中英

How to get the variable name of DocVariable?

I have a word document that is filled with DocVariables. What I am having trouble with is that I can't retrieve their names. What works for me now is this;

foreach(Field ff in aDoc.Fields)
{
    string txt = ff.Code.Text;
    //parse...
    //it returns {DOCVARIABLE PageCount \* MERGEFORMAT } but what I want is just PageCount.
}

I tried this;

foreach (Field ff in aDoc.Fields)
{
    if (ff.Type == WdFieldType.wdFieldDocVariable)
    {
        ff.Select();
        string s = ff.Result.Text;
    }
}

but it returns null. Can you please help me with this problem or is this what it should be like?

如果您知道了,如您所说的字符串{DOCVARIABLE PageCount \\* MERGEFORMAT } ,则可以执行以下操作:

string str = txt.substring(1, txt.IndexOf(" "));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM