Word 2003文書に差し込みフィールド交換し、スタイルをキープ
-
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
}
}
私はまたget_styleとset_styleなくないexentにを使用して、選択中のCopyFormatとPasteFormat方法を使用してみました。
解決
の代わりに、あなたの選択の上TypeText使用しての分野のResultプロパティを使用します:
if (values.ContainsKey(fieldName))
{
mergeField.Result = (values[fieldName]);
}
このフィールド内の任意の書式が保持されるようになります。
所属していません StackOverflow