问题描述
我试图写查找/替换code通过Interop.Word(11.0)使用Word自动化Word文档。我的文件都有各个领域(指不以Document.Fields显示),其周围有括号,例如,<日期>
需要被替换 DateTime.Now.Format(MM / DD / YYYY)
。查找/替换工作正常。然而,一些被替换文本右对齐,并且在更换时,文本换行到下一行。有没有什么办法,我可以保证的理由,当我进行更换? code是如下:
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。
推荐答案
可以Microsoft.Office.Interop.Word.WdParagraphAlignment对准
You can Microsoft.Office.Interop.Word.WdParagraphAlignment for alignment
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object missing = System.Type.Missing;
try
{
object fileName = @"C:\TT\change.doc";
doc = word.Documents.Open(ref fileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
doc.Activate();
foreach (Microsoft.Office.Interop.Word.Range tmpRange in doc.StoryRanges)
{
tmpRange.Find.Text = "<DATE>";
tmpRange.Find.Replacement.Text = DateTime.Now.ToString("MM/dd/yyyy");
tmpRange.Find.Replacement.ParagraphFormat.Alignment =
Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
tmpRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);
}
doc.Save();
doc.Close(ref missing, ref missing, ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);
}
catch (Exception ex)
{
doc.Close(ref missing, ref missing, ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);
}
这篇关于使用Interop.Word,有没有办法做一个替代(用Find.Execute),并保持原文的理由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!