使用Microsoft Word互操作的C#程序

Sanket Naik

Word文档: word.doc

One

Two

three

使用Microsoft Word互操作的C#程序

 using System;
 using Microsoft.Office.Interop.Word;

 class Program
 {
   static void Main()
 {
    // Open a doc file.
    Application application = new Application();
    Document document = application.Documents.Open("C:\\word.doc");

    // Loop through all words in the document.
    int count = document.Words.Count;
    for (int i = 1; i <= count; i++)
    {
        // Write the word.
        string text = document.Words[i].Text;
        Console.WriteLine("Word {0} = {1}", i, text);
    }
    // Close word.
    application.Quit();
  }
  }

输出:

Word 1 = One
Word 2 =
Word 3 = Two
Word 4 =
Word 5 = three
Word 6 =
贾兹布

试试这个-为real单词添加另一个计数器

    internal class Program
    {
        private static void Main(string[] args)
        {
            // Open a doc file.
            Application application = new Application();
            Document document = application.Documents.Open("C:\\temp\\word.doc");

            // Loop through all words in the document.

            int k = 1;

            int count = document.Words.Count;
            for (int i = 1; i <= count; i++)
            {
                // Write the word.
                string text = document.Words[i].Text.Trim();

                if (!string.IsNullOrEmpty(text))
                {
                    Console.WriteLine("Word {0} = {1}", k, text);
                    k++;
                }
            }

            Console.ReadLine();
            // Close word.
            application.Quit();
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Excel 未使用 C# 中的互操作更新

如何使用C#替换Microsoft WORD中的双引号

使用参考C#添加Microsoft.Office.InterOp.Word

Java程序使用Microsoft Word打开PDF

C#不能跨程序集边界使用,因为它具有作为嵌入式互操作类型的通用类型参数

使用Microsoft Word进行翻译

如何使用C#和Excel互操作访问Excel属性(如创建日期)

如何使用C#互操作对Excel中的列进行重新排序

如何在没有互操作程序集的情况下将任何文件类型嵌入到Microsoft Word中

使用搜索Microsoft Graph API C#

C#/ F#互操作性:不能在F#代码中使用C#类型

如何使用 C# 在 Microsoft Word 的文本框中将特定单词加粗?

如何使用 Microsoft.Office.Interop.Word 在 C# 中编辑 docx 文件

如何使用 Microsoft Word Interop Bookmarks C# 选中一个框

我如何使用OpenXML C#水平合并Microsoft Word表单元格

使用Interop.Word使用Visual Studio C#程序锁定Word字段-超出范围

C#中的互操作字应用程序错误

在Microsoft Word中强制使用样式

如何使用各种大小的标题-Microsoft Word

如何使用Microsoft Word创建Web表单?

使用Microsoft Word插入©(版权符号)

C# 使用未知模块中的互操作未处理的“System.IO.FileNotFoundException”加载 Excel 文件

是否可以在 Microsoft Word 文档中的特定位置插入图标,在 C# 中使用 Aspose.Word

使用 Microsoft.Graph 列出带有 C# 控制台应用程序的日历

使用 Microsoft.Web.Administration 将应用程序池添加到 iis C#

Microsoft 是否在简单的 C 程序中使用 TCP 连接?

与GetNamedPipeClientComputerName的C#互操作

MsiSourceListEnumSources互操作C#

如何使用 Microsoft bot 框架 SDK3 C# 在 Microsoft 团队中显示欢迎消息