You must Sign In to post a response.
  • Category: ASP.NET

    How to read word file without opening using C#?

    Hello,


    In my application I will receive multiple word files from different locations,I will have to read these without opening the word file as it consumes more memory.

    I have used Microsoft.Office.Interop.Word dll for reading.

    Any help appreciated.

    Thanks
  • #747519
    Hi,
    Please check below sample code to read word document,

    Microsoft.Office.Interop.Word.ApplicationClass objWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
    object file = "C:\\Test\\Test.doc";
    object objNull = System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Word.Document objDoc = objWordApp.Documents.Open(ref file, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);
    objDoc.ActiveWindow.Selection.WholeStory();
    objDoc.ActiveWindow.Selection.Copy();
    IDataObject data = Clipboard.GetDataObject();
    string strWordText = data.GetData(DataFormats.Text).ToString();
    objDoc.Close(ref objNull, ref objNull, ref objNull);
    objWordApp.Quit(ref objNull, ref objNull, ref objNull);
    Response.Write(strWordText);


    Word always consume bit high memory if you have to do multiple word operation then do it in the background using threading concepts.


    Regards,
    Asheej T K

  • #747527
    Thank you.I will try to use threading concepts.Do you have sample code?

  • #769262
    Is it possible to read without clipboard

  • #769264
    Hi,

    You can also use Spire.Doc to read the word file. The code is very simple.
    Document doc = new Document();
    doc.LoadFromFile("Sample.docx");
    string s = doc.GetText();
    File.WriteAllText("Extract.txt", s.ToString());

  • #769266
    Hi,

    You may use interop dll or streamreader to accomplish your task.

    Are you facing any issue while using above code?


    Regards,
    Asheej T K

  • #769277
    @asjeeh: I'm trying to read using interop. But couldn't read merged cells. Facing issue "cannot access individual rows in this collection because the table has vertically merged cells". How to read merged cells or how to change the merge cells into unmerge table as below

    Code tried: string text= table.Rows[0].Range.Text;

    Name Gender Results IssueId
    Male Female
    Reynold - Yes Available SK0056


    Expected results(by removing the cell value 'Gender')

    Name Male Female Results IssueId
    Reynold - Yes Available SK0056

  • #769279
    hi

    please check this code may be it help you



    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
    object nullObject = System.Reflection.Missing.Value;
    object file = @"C:\doc.doc";
    Microsoft.Office.Interop.Word.Document doc = app.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);
    doc.ActiveWindow.Selection.WholeStory();
    doc.ActiveWindow.Selection.Copy();
    IDataObject data = Clipboard.GetDataObject();
    string text = data.GetData(DataFormats.Text).ToString();
    doc.Close(ref nullObject, ref nullObject, ref nullObject);
    app.Quit(ref nullObject, ref nullObject, ref nullObject);

  • #769282
    You can use OPENXmlSdk to read word file, if you are use web based application then you need to install word on server to use INTEROP object and that is does not recommended by Microsoft.
    Openxml is a good and approach to get the task done.
    lookout below links
    https://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK
    https://www.codeproject.com/Tips/994905/Edit-Word-Documents-using-OpenXML-and-Csharp-Witho
    https://www.codeproject.com/Articles/1043875/Create-Word-table-using-OpenXML-and-Csharp-Without

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments