How can I read the contents (separate paragraphs) of a .doc /.docx file in C#

Dani AI

Generated

A few practical options and gotchas tied to the replies already here.

As pointed out, Office Interop (Microsoft.Office.Interop.Word) really automates the Word application and therefore requires Word to be installed. That route works for desktop utilities, but Microsoft warns that Office automation is not designed for unattended or server-side use (modal dialogs, user-profile assumptions, scalability and stability problems). (support.microsoft.com)

For .docx files the most robust, long-term approach is the Open XML SDK: it opens the DOCX package directly (no Word process) and lets code iterate paragraphs, runs and text nodes. Example (read-only):

using System.Collections.Generic;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

List<string> ReadParagraphs(string path)
{
    var paragraphs = new List<string>();
    using (var doc = WordprocessingDocument.Open(path, false))
    {
        var body = doc.MainDocumentPart.Document.Body;
        foreach (var p in body.Elements<Paragraph>())
            paragraphs.Add(p.InnerText);
    }
    return paragraphs;
}

This pattern and related guidance are in the Open XML docs. (learn.microsoft.com)

For legacy binary .doc files either convert to .docx (when possible) or use a library that understands the binary format. Options that avoid automating Word: DocX (simple API for .docx), Aspose.Words (commercial — supports DOC and DOCX without Word), and NPOI (open-source, POI port with DOCX and partial DOC support). Each has tradeoffs (license, accuracy, performance). (github.com)

Troubleshooting tips: if Interop must be used on a desktop, open read-only, set Visible=false and DisplayAlerts=none, always call Quit and release COM objects (Marshal.ReleaseComObject + GC.Collect), and run automation on an STA thread. For unattended or server apps prefer Open XML or a server-safe library — as suggested, third-party components are often the easiest route; ’s caution about reviving old threads is noted, but the clarifications above should help future readers.

Recommended Answers

All 7 Replies

Very easy...
Project->Add Reference->COM
tab and look for the Microsoft Word X.x Object Library
Use ApplicationClass class. Enjoy...

I have added the reference, but could you be more elaborative with the ApplicaitonClass usage?

No, I need you to try using it, I could tell you some code and your problem been solved! but I need you to try to learn. Please try

Is there a way I could run it in background. i.e. without opening the word file.

Have manage to read the contents but it one can certainly notice the opening and closing of the world doc

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

object fileNameO = fileName;//"c:\\sample.doc";
object objFalse = false;
object objTrue = true;
object missing = System.Reflection.Missing.Value;
object emptyData = string.Empty;

try

{
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref fileNameO, ref objFalse, ref objTrue,
ref objFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue,
ref missing, ref missing, ref missing, ref missing);


aDoc.ActiveWindow.Selection.WholeStory();
aDoc.ActiveWindow.Selection.Copy();



IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
String filetext = data.GetData(System.Windows.Forms.DataFormats.Text).ToString();
System.Windows.Forms.Clipboard.SetDataObject(string.Empty);
richTextBox1.Text = filetext;
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}

finally

{
wordApp.Documents.Close(ref missing, ref missing, ref missing);
wordApp.Application.Quit(ref missing, ref missing, ref missing);
}
}

No, I need you to try using it, I could tell you some code and your problem been solved! but I need you to try to learn. Please try

Look .doc file is binary file I think you can't do that without word, but .docx is follow OOXML you can play with it.

Hello,
read the contents (separate paragraphs) of a .doc /.docx file in C#.
there is two way to do this, first way is use office Interop, The office must be installed on the machine, Microsoft does not recommend using Office application in server-side scenarios.
Second way to use 3 party component, I used Spire.Doc.

Was it really necessary to resurrect a thread over a year and a half old just to pitch a .net component?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.