How to insert a docVariable field without changing the Word source content? RRS feed

  • Question

  • Hi all,

    in a recent project, I need to insert some kind of markers in the Word source document without changing the content/text. I end up using doc Variable to do that and it works until hitting the use case when track changing is enabled. When adding the doc Variable to the document, I need to specify a value, which should be the text of the range of a place of interest, e.g., a bookmark, in the source document, since I don't want to change the content. After adding the doc Variable to the range as a field, Word automatically replaces the range text with the doc Variable value, which is actually the same as the range text. However, when the track change flag is on, the color and style (e.g., underline) will be changed and modification on the source document is noticeable, as shown in the screenshot below.


    The followings are the related C# code. Note that the lines to add underline and change text color won't work for an unknown reason. Please let me know if there is a way to fix this issue or if anything is wrong or there is an alternative solution without using docVariable to achieve what I need. Thanks in advance!

    wordDocument.TrackRevisions = false;

    wordDocument.ActiveWindow.ActivePane.View.RevisionsFilter.Markup = WdRevisionsMarkup.wdRevisionsMarkupNone;

    //suppose the current range is for the heading "ANALYSE DE MARCHE" and the marker we want to embed is "docVarName", i.e., the name of the docVariable

    wordDocument.Variables.Add(docVarName, range.Text);

    Object obj = range.get_Style();

    wordDocument.Fields.Add(range, WdFieldType.wdFieldDocVariable, docVarName, true);


    range.Font.Underline = WdUnderline.wdUnderlineSingle;

    range.Font.ColorIndex = WdColorIndex.wdDarkRed;


    • Edited by ChangSun Thursday, November 15, 2018 10:47 PM
    Thursday, November 15, 2018 10:46 PM

All replies