How to get text from line number in MS Word

前端 未结 3 1298
迷失自我
迷失自我 2021-01-22 08:00

Is it possible to get text (line or sentence) from a given line number in MS Word using office automation? I mean its ok if I can get either the text in the given line number or

3条回答
  •  广开言路
    2021-01-22 08:51

    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;
    

提交回复
热议问题