我尝试运行来自 dotnetpearls.com 的示例程序,起初该程序根本无法运行。
显然,在启动应用程序对象之前,我必须以管理员身份运行 VS Express 2012。之后,下次出错时,就是我尝试打印文档中的文本时。错误发生在 string text = doc.Words[i].Text;
using System;
using Microsoft.Office.Interop.Word;
namespace WordTestProgram
{
class Program
{
static void Main(string[] args)
{
Application app = new Application();
Document doc = app.Documents.Open("C:\\word.doc");
int count = doc.Words.Count;
for (int i = 0; i <= count; i++)
{
string text = doc.Words[i].Text;
Console.WriteLine("Word {0} = {1}",i,text);
}
app.Quit();
}
}
}
我知道我试图从中提取数据的文档确实有 3 个单词和 3 个空格。所以它不是空的。
请您参考如下方法:
我自己找到了答案
代替:int i = 0; i <= count; i++
我应该做的:int i = 1; i <= count; i++
显然数组中的成员 0 为空,程序无法处理。
