EXTRACT TEXT FROM OPENED MICROSOFT WORD DOCUMENT

Mark Devid

Total Post:102

Points:714
Posted by  Mark Devid
C# 
C#
 1083  View(s)
Ratings:
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?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Extract text from opened Microsoft Word document

    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.

      Modified On Apr-04-2018 12:50:18 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!