【问题标题】:Using Interop.Word, is there a way to do a replace (using Find.Execute) and keep the original text's justification?使用 Interop.Word,有没有办法进行替换(使用 Find.Execute)并保留原始文本的理由?
【发布时间】:2010-05-18 16:49:08
【问题描述】:

我正在尝试通过 Interop.Word (11.0) 使用 Word 自动化为 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。

【问题讨论】:

    标签: c# .net ms-word office-interop word-automation


    【解决方案1】:

    您可以通过 Microsoft.Office.Interop.Word.WdParagraphAlignment 进行对齐

            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);
            }
    

    【讨论】:

    • 应用程序退出后使用 Marshal.ReleaseComObject(your object) 从内存释放对象。
    猜你喜欢
    • 2021-05-08
    • 2021-01-09
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多