doc (docx ではなく、実際の doc) ファイルを生成する必要があります。私が見つけた「最良の」方法は、ワード オートメーション (Word 2010) を使用することです。私が開いているファイルがあり、新しい名前で保存する前に内部の値を置き換えます。(例: 「CHRONO」を「155023」に置き換えます)。これを行うには、Application.Selection.Find を使用します。新しい値が 255 文字を超える場合に問題が発生しました (Microsoft の制限...)。この問題を回避するために、この場合は TypeText を使用します。私の問題は、TypeText を使用すると、Replace が機能しなくなることです。そして、私はその理由を見つけることができません。どんなアイデアでも大歓迎です。
私のコードは関数内にあり、foreach で各値を置き換えて呼び出されます。
private void Replace(Application app, string name, string newValue)
{
Selection selection = app.Selection;
Find find = selection.Find;
Replacement replacement = find.Replacement;
find.ClearFormatting();
find.Text = "<" + name + ">";
// Word limitation : can't replace with more than 255 characters,
// use another way to do it if that's the case
if (tempNewValue.Length < 255)
{
replacement.ClearFormatting();
replacement.Text = tempNewValue;
find.Execute(Replace: replaceAll);
}
else
{
while (find.Execute())
{
selection.TypeText(tempNewValue);
}
}
Marshal.ReleaseComObject(replacement);
Marshal.ReleaseComObject(find);
Marshal.ReleaseComObject(selection);
}