Word 2003 문서에서 Mergefields를 교체하고 스타일을 유지하십시오.
-
20-09-2019 - |
문제
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
}
}
선택에서 copyformat 및 pasteformat 메소드를 사용해 보았습니다. get_style 및 set_style을 사용하지만 예외는 없습니다.
해결책
선택의 상단에 typetext를 사용하는 대신 필드의 결과 특성을 사용하십시오.
if (values.ContainsKey(fieldName))
{
mergeField.Result = (values[fieldName]);
}
이렇게하면 현장의 모든 서식이 유지됩니다.
제휴하지 않습니다 StackOverflow