none
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.

    Image

    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.set_Style(obj);

    range.Font.Underline = WdUnderline.wdUnderlineSingle;

    range.Font.ColorIndex = WdColorIndex.wdDarkRed;

    range.Fields.Update();





    • Edited by ChangSun Thursday, November 15, 2018 10:37 PM
    Thursday, November 15, 2018 10:32 PM

All replies

  • Hi ChangSun,

    Please refer to this post.

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Friday, November 16, 2018 6:53 AM
    Moderator
  • Hi Yuki, thanks for the response. Unfortunately, there might be a misunderstanding, and that won't resolve my issue. Please see my comment in that post for details. 

    • Edited by ChangSun Friday, November 16, 2018 9:52 PM
    Friday, November 16, 2018 4:57 PM