Home > DeveloperSection > Forums > Extract text from opened Microsoft Word document
Mark Devid
Mark Devid

Total Post:102

Points:714
Posted on    December-23-2013 2:50 AM

 C# C# 
Ratings:


 1 Reply(s)
 714  View(s)
Rate this:

I need to extract text (using C#, VS2012) from opened word document. I keep receiving an error that 'cannot access file, because it is being used by another process'. Is there any work around this? So I can extract text programmatically when document is opened in Microsoft Word?



Pravesh Singh

Total Post:411

Points:2881
Posted on    December-23-2013 5:09 AM

Hi Mark,

If you cannot open the document programmatically, I would connect to the running instance of Word, get a handle to the document, and ask for the text in it. Like this:

using System;

using Microsoft.Office.Interop.Word;

namespace ConsoleApplication12

{

    class Program

    {

        static void Main(string[] args)

        {

            var wordApp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

            var words = wordApp.ActiveDocument.Words;

            foreach (Range word in words)

            {

                Console.WriteLine(word.Text);

            }

        }

    }

}

Remember to reference the Word Interop assembly.


Don't want to miss updates? Please click the below button!

Follow MindStick