I am trying to create a library to replace MergeFields in a Word 2003 document, everything works fine, except that I lose the style applied to the field when I replace it, is there any way to save it?
This is the code I use to replace the fields:
private void FillFields2003(string template, Dictionary<string, string> values)
{
object missing = Missing.Value;
var application = new ApplicationClass();
var document = new Microsoft.Office.Interop.Word.Document();
try
{
foreach (Field mergeField in document.Fields)
{
if (mergeField.Type == WdFieldType.wdFieldMergeField)
{
string fieldText = mergeField.Code.Text;
string fieldName = Extensions.GetFieldName(fieldText);
if (values.ContainsKey(fieldName))
{
mergeField.Select();
application.Selection.TypeText(values[fieldName]);
}
}
}
document.Save();
}
finally
{
}
}
I tried using the CopyFormat and PasteFormat methods in the selection, also using get_style and set_style, but without exent.
source
share