Open and read Word file in C# and VB.NET

Open and read Word (DOCX) and Word 2003 (DOC) files in C# and VB.NET with GemBox.Document component.

GemBox.Document is a C# / VB.NET component that enables developers to read, write, convert and print document files (DOCX, DOC, PDF, HTML, XPS, TXT) from .NET applications in a simple and efficient way without the need for Microsoft Word on either the developer or client machines.
GemBox.Document Free is free of charge while GemBox.Document Professional is a commercial version licensed per developer.
For more information, see GemBox.Document features and why GemBox.Document outperforms Word automation.

Following example opens a Word file (DOCX or DOC), reads document textual content and writes it to a console.

C# code

// Load DOCX or DOC document.
var document = DocumentModel.Load(isDocx ? "Document.docx" : "Document.doc");

// Iterate over all paragraphs in the document.
foreach (Paragraph para in document.GetChildElements(true, ElementType.Paragraph))
{
// Iterate over all runs in the paragraph and write their text to Console.
foreach (Run run in para.GetChildElements(true, ElementType.Run))
Console.Write(run.Text);
Console.WriteLine();
}

VB.NET code

' Load DOCX or DOC document.
Dim document = DocumentModel.Load(If(isDocx, "Document.docx", "Document.doc"))

' Iterate over all paragraphs in the document.
For Each para As Paragraph In document.GetChildElements(True, ElementType.Paragraph)
' Iterate over all runs in the paragraph and write their text to Console.
For Each run As Run In para.GetChildElements(True, ElementType.Run)
Console.Write(run.Text)
Next
Console.WriteLine()
Next

For a more detailed example, see Reading sample from GemBox.Document Sample Explorer.