使用 Interop.Word,有没有办法进行替换(使用 Find.Execute)并保留原始文本的理由?
我正在尝试通过 Interop.Word (11.0) 使用 Word Automation 为 Word 文档编写查找/替换代码。我的文档都有各种用括号括起来的字段(不会显示在 Document.Fields 中),例如,
需要替换为 DateTime.Now。格式(“月/日/年”)
。查找/替换工作正常。但是,某些被替换的文本是右对齐的,并且替换后,文本会换行到下一行。有什么方法可以在执行替换时保留理由吗?代码如下:
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。
I'm attempting to write find/replace code for Word documents using Word Automation through Interop.Word (11.0). My documents all have various fields (that don't show up in Document.Fields) that are surrounded with brackets, eg., <DATE>
needs to be replaced with DateTime.Now.Format("MM/dd/yyyy")
. The find/replace works fine. However, some of the text being replaced is right-justified, and upon replacement, the text wraps to the next line. Is there any way that I can keep the justification when I perform the replace? Code is below:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过 Microsoft.Office.Interop.Word.WdParagraphAlignment 进行对齐
You can Microsoft.Office.Interop.Word.WdParagraphAlignment for alignment