I tried to run a sample program from dotnetpearls.com and at first the program didn't work at all.
Apparently I had to run VS Express 2012 as Administrator, before I could start an Application object. After that, the next time it errors out, is when I try to print out the text from the document. Error happens at 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();
}
}
}
I know for a fact that the document I am trying to extract data from, does have 3 words and 3 spaces in it. So it's not empty.
I found the answer myself
Instead of: int i = 0; i <= count; i++
I should do: int i = 1; i <= count; i++
Apparently member 0 in the array is null and the program can't handle that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With