C#读取Word并在指定位置插入文本

2021-04-22 08:26

阅读:571

标签:miss   oid   key   using   pac   off   class   分享   type   

 

using System;
using System.IO;
using System.Reflection;
using MWord = Microsoft.Office.Interop.Word;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            MWord.Application app = new Microsoft.Office.Interop.Word.Application();
            MWord.Document doc = null;
            object unknow = Type.Missing;
            app.Visible = true;
            string str = @"F:\2.doc";
            object file = str;
            doc = app.Documents.Open(ref file,
                ref unknow, ref unknow, ref unknow, ref unknow,
                ref unknow, ref unknow, ref unknow, ref unknow,
                ref unknow, ref unknow, ref unknow, ref unknow,
                ref unknow, ref unknow, ref unknow);


            //循环输出所有内容
            for (int i = 1; i )
            {
                string temp = doc.Paragraphs[i].Range.Text.Trim();
                Console.WriteLine(temp);
            }


            object bk = "电话";       //word书签
            if (doc.Bookmarks.Exists("电话"))
            {
                doc.Bookmarks.get_Item(bk).Range.Text = "insert text"; // 插入文本
            }

            Console.ReadKey();

        }
    }
}

 

 

技术分享图片

 

C#读取Word并在指定位置插入文本

标签:miss   oid   key   using   pac   off   class   分享   type   

原文地址:http://www.cnblogs.com/xinyibufang/p/8022374.html


评论


亲,登录后才可以留言!