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

    Reading a Doc File in C#

    Hi,

    I have a program written in C# where I am displaying the content after reading a .rtf file.
    My code is given below:
    Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
    object miss = System.Reflection.Missing.Value;
    object path = System.Windows.Forms.Application.StartupPath + "\\FormulaMaster.rtf";
    object readOnly = true;
    Microsoft.Office.Interop.Word.Documentdoc = 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 = "";

    doc.ActiveWindow.Selection.WholeStory();
    doc.ActiveWindow.Selection.Copy();
    IDataObject data = Clipboard.GetDataObject();
    richTextBox1.Text = data.GetData(DataFormats.Text).ToString();

    But what I need is . After closing the form, when I edit the rft file, It is not allowing to save in the same name in same path.
    Bu my application only reed the specific file from a specific path.

    Kindly help on it.


    Thank You,
    Binay Kumar Prasad.
  • #765209
    Hi,
    Try closing the doc and word object after you done with reading that document.
    doc.Close(ref miss, ref miss, ref miss);
    doc = null;
    word = null;
    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #765219
    COM/Interop objects are very heavy and unmanaged objects, GC alone can not able to collect them and dispose them, we SHOULD release objects after we have use them, finally is the recommended place to release all com objects, Close() and quit() methods are used to release Document and Application object respectively. see below snippet

    finally
    {
    //Winword is heavy and unmanaged object, you should release all resources used for it
    if (objDoc != null)
    {
    objDoc.Close();
    objDoc = null;
    }
    if (objApp != null)
    {
    objApp.Quit();
    objApp = null;
    }
    }

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

  • #765259
    Hi

    You can try this code for reading word text working good


    First Refer word dll -> Microsoft.Office.word

    then try this code.

    Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
    object miss = System.Reflection.Missing.Value;
    object path = @"D:\12\Jesus.docx";
    object readOnly = true;
    Microsoft.Office.Interop.Word.Document 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 = "";
    for (int i = 0; i < docs.Paragraphs.Count; i++)
    {
    totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
    }
    lblWords.Text = totaltext.ToString();
    docs.Close();
    word.Quit();

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments