Home
Map
Word Interop: Microsoft.Office.Interop.WordUse Microsoft.Office.Interop.Word. Open a DOC file and read the text in it.
C#
This page was last reviewed on Sep 25, 2022.
Word. We have a Microsoft Word document and want to read it in a C# program. With the Microsoft.Office.Interop.Word assembly, we get the contents and formatting from the document.
Getting started. In Visual Studio, please add the Microsoft.Office.Interop.Word assembly to your project. Go to Project and then Add Reference.
Excel Interop
Example. First, our file contains 3 paragraphs containing one word each. The program instantiates an Application instance and then we call Documents.Open on that variable.
Next We loop through the Words collection and read the Text property on each element. We then display and call Quit.
for
using System; using Microsoft.Office.Interop.Word; class Program { static void Main() { // Open a doc file. Application application = new Application(); Document document = application.Documents.Open("C:\\word.doc"); // Loop through all words in the document. int count = document.Words.Count; for (int i = 1; i <= count; i++) { // Write the word. string text = document.Words[i].Text; Console.WriteLine("Word {0} = {1}", i, text); } // Close word. application.Quit(); } }
Word 1 = One Word 2 = Word 3 = Two Word 4 = Word 5 = three Word 6 =
Empty paragraphs. The empty paragraphs in the input file are considered words. If you have multiple words in a paragraph, they will each be separate in the Words collection.
So In Interop.Word, a paragraph is made up of a collection of one or more words.
Quit. Why is the application.Quit statement important? If you don't include this, the WINWORD.EXE application will remain in the process list.
Then When this program is run again, a new one will be started. This wastes memory.
Note It is important to iterate on Words from 1 to Count inclusive. Correction suggested by Robert Ford.
Summary. We looked at the Microsoft.Office Interop.Word assembly and learned how to read in data from a Word document. This can be useful when you have DOC or DOCX files.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 25, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.