Share via

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

Anonymous
2018-11-09T03:10:42+00:00

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 docVariable to do that and it works until hitting the use case when track changing is enabled. When adding the docVariable 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 docVariable to the range as a field, Word automatically replaces the range text with the docVariable value, which is actually the same as the range text. However, when 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.set_Style(obj);

range.Font.Underline = WdUnderline.wdUnderlineSingle;

range.Font.ColorIndex = WdColorIndex.wdDarkRed;

range.Fields.Update();

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2018-11-17T06:08:22+00:00

Given a range, the following codes are not working.

range.Font.Underline = WdUnderline.wdUnderlineSingle;

range.Font.ColorIndex = WdColorIndex.wdDarkRed;

When working with fields, the font attributes are managed by applying either:

• the required formatting to the entire field, including the field braces (i.e. not to just the field's result); or

• adding a Charformat switch to the field (delete any Mergeformat switch that's present) and applying the desired formatting to at least the field code's first character (e.g. { DOCVARIABLE MyVar \* Charformat }).

Neither approach allows you to apply formatting to only part of the field. Any other approach will result in the formatting being lost when the field is refreshed.

Was this answer helpful?

0 comments No comments

28 additional answers

Sort by: Most helpful
  1. Anonymous
    2018-11-12T05:36:58+00:00

    I still do not know what it is that you are doing, so I cannot comment.

    Hi Doug,

    so to be specific, what I need to do is to dynamically track the location change of bookmarks for different versions of Word documents. The application is used on a Windows server. For example, the user uploads document version 1 to the server with bookmark1, and then changes the name from bookmark1 to bookmark11 and moves it to a different line in version 2 and upload that version to the server, I need to track the name and line changes. So I need some kind of marker like a docVariable to actually be embedded in version 1 to dynamically track all the changes made to the following versions.

    Any advice will be helpful. Thanks again.

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2018-11-09T22:11:07+00:00

    I still do not know what it is that you are doing, so I cannot comment.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2018-11-09T15:26:46+00:00

    Hi Doug

    thanks a lot for the reply. So the reason why I need to create docVariable fields is that I need some kind of marker like a bookmark to actually embed in the source document to dynamically track the location. Is there another way to do this to get around with the track change issue? 

    Regarding the code, I have two questions:

    1. is there a way to really keep all the formatting of the range text when creating a docVariable field the way I am doing? I have to save the style and reset it after the Fields.Add() operation.

    2) why the code to add underline and change text color is not working?

    Thanks again!

    Without the complete picture of what you are doing, the only observation that I can make is that it is unusual to be adding docvariable fields to a document, rather that having them in the template from which the document is created.  Under those circumstances, change tracking is unlikely to be an issue.

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2018-11-09T03:30:45+00:00

    Without the complete picture of what you are doing, the only observation that I can make is that it is unusual to be adding docvariable fields to a document, rather that having them in the template from which the document is created.  Under those circumstances, change tracking is unlikely to be an issue.

    Was this answer helpful?

    0 comments No comments