This helps in you getting string line by line.
object file = Path.GetDirectoryName(Application.ExecutablePath) + @"Answer.doc";
Word.Application wordObject = new Word.ApplicationClass();
wordObject.Visible = false;
object nullobject = Missing.Value;
Word.Document docs = wordObject.Documents.Open
(ref file, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject);
String strLine;
bool bolEOF = false;
docs.Characters[1].Select();
int index = 0;
do
{
object unit = Word.WdUnits.wdLine;
object count = 1;
wordObject.Selection.MoveEnd(ref unit, ref count);
strLine = wordObject.Selection.Text;
richTextBox1.Text += ++index + " - " + strLine + "
"; //for our understanding
object direction = Word.WdCollapseDirection.wdCollapseEnd;
wordObject.Selection.Collapse(ref direction);
if (wordObject.Selection.Bookmarks.Exists(@"EndOfDoc"))
bolEOF = true;
} while (!bolEOF);
docs.Close(ref nullobject, ref nullobject, ref nullobject);
wordObject.Quit(ref nullobject, ref nullobject, ref nullobject);
docs = null;
wordObject = null;
Here's the genius behind the code. Follow the link for some more explanation on how it works.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…