小编Dyl*_*lan的帖子

C# 在 Word 文档中查找和替换文本的另一种方法

我正在寻找替代方法来搜索 word 文档中的某些文本,然后将其替换为另一个文本。我目前使用 Find and Replace 方法工作,但是我想知道是否还有其他方法可以做到这一点。

我尝试过的一种方法是逐段浏览并搜索文本,将其替换,然后将其粘贴到新的 Word 文档中并保存。然而,当涉及到图像、教科书、表格等时,这使事情变得更加复杂。而且格式没有得到保留,所以这是另一个问题。

我目前的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace Test
{
    static class Program
    {
        static void Main()
        {
            //Create a new microsoft word file
            Microsoft.Office.Interop.Word.Application fileOpen = new Microsoft.Office.Interop.Word.Application();
            //Open a already existing word file into the new document created
            Microsoft.Office.Interop.Word.Document document = fileOpen.Documents.Open(@"C:\Users\dpatel\Desktop\Test1.docx", ReadOnly: false);
            //Make the file visible 
            fileOpen.Visible = true;
            document.Activate();
            //The FindAndReplace takes the text to find under any formatting …
Run Code Online (Sandbox Code Playgroud)

.net c# interop ms-word

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

.net ×1

c# ×1

interop ×1

ms-word ×1