Interop.Word(11.0)を介してWord Automationを使用して、Word文書の検索/置換コードを作成しようとしています。私のドキュメントにはすべて、角かっこで囲まれたさまざまなフィールド(Document.Fieldsには表示されません)があります。たとえば、<DATE>
に置き換える必要がありますDateTime.Now.Format("MM/dd/yyyy")
。検索/置換は正常に機能します。ただし、置き換えられるテキストの一部は右寄せされており、置き換えられると、テキストは次の行に折り返されます。交換を実行するときに正当化を維持できる方法はありますか?コードは以下のとおりです。
using Word = Microsoft.Office.Interop.Word;
Word.Application wordApp = null;
try
{
wordApp = new Word.Application {Visible = false};
//.... open the document ....
object unitsStory = Word.WdUnits.wdStory;
object moveType = Word.WdMovementType.wdMove;
wordApp.Selection.HomeKey(ref unitsStory, ref moveType);
wordApp.Selection.Find.ClearFormatting();
wordApp.Selection.Find.Replacement.ClearFormatting(); //tried removing this, no luck
object replaceTextWith = DateTime.Now.ToString("MM/dd/yyyy");
object textToReplace = "<DATE>";
object replaceAll = Word.WdReplace.wdReplaceAll;
object typeMissing = System.Reflection.Missing.Value;
wordApp.Selection.Find.Execute(ref textToReplace, ref typeMissing,
ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing,
ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing,
ref replaceTextWith, ref replaceAll, ref typeMissing, ref typeMissing,
ref typeMissing, ref typeMissing);
// ... save quit etc....
}
finally
{
//clean up wordApp
}
TIA。