wil*_*lvv 4 c# interop vsto ms-word mergefield
我一直在尝试创建一个库来替换Word 2003文档中的MergeFields,一切正常,但是当我替换它时,我失去了应用于该字段的样式,有没有办法保留它?
这是我用来替换字段的代码:
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
{
// Open the file
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
{
// Release resources
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试在选择中使用CopyFormat和PasteFormat方法,也使用get_style和set_style但不使用exent.
不要在选择的顶部使用TypeText,而是使用Field的Result属性:
if (values.ContainsKey(fieldName))
{
mergeField.Result = (values[fieldName]);
}
Run Code Online (Sandbox Code Playgroud)
这将确保保留字段中的任何格式.
| 归档时间: |
|
| 查看次数: |
4232 次 |
| 最近记录: |