使用 Interop.Word,有没有办法进行替换(使用 Find.Execute)并保留原始文本的理由?

发布于 2024-09-02 11:29:48 字数 1399 浏览 5 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-09-09 11:29:48

您可以通过 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);
        }

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);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文