How to get text from line number in MS Word

北城余情 提交于 2019-12-02 02:16:54

Fortunately after some epic searching I got a solution.

    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 + "\r\n"; //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.

Use this if you want to read standard text .txt files Here is something that you can use to read the files with one call

List<string> strmsWord = 
    new List<string>(File.ReadAllLines(yourFilePath+ YourwordDocName));

if you want to loop thru and see what the items that were returned use something like this

 foreach (string strLines in strmsWord )
 {
   Console.WriteLine(strLines);
 }     

or

I totally forgot about something Word docs are probably in binary format so look at this and read the contents into a RichTextBox and from there you could either get at the line number you want or load it into a list after words.. this link will show you Reading from a Word Doc if you want to read the XML Formatting of the word Document: here is a good link as to checkout as well ReadXML Format of a Word Document

This onne is an even easier example reads contents into the ClipBoard Load Word into ClipBoard

shiba
var word = new Word.Application();
object miss = Missing.Value;
object path = @"D:\viewstate.docx";
object readOnly = true;
var docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, 
                               ref miss, ref miss, ref miss, ref miss, ref miss, 
                               ref miss, ref miss, ref miss, ref miss, ref miss, 
                               ref miss, ref miss);
string totaltext = "";

object unit = Word.WdUnits.wdLine;
object count = 1;
word.Selection.MoveEnd(ref unit, ref count);
totaltext = word.Selection.Text;

TextBox1.Text = totaltext;
docs.Close(ref miss, ref miss, ref miss);
word.Quit(ref miss, ref miss, ref miss);
docs = null;
word = null;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!