We help IT Professionals succeed at work.
Get Started

Convert word document to text using c# running ASP.net

984 Views
Last Modified: 2015-03-15
I have a web application built using c#, asp.net.  I need to convert an uploaded word document to a text string.  I have looked at microsoft.office.interop.word (which works locally), but when I run on a server (where word is not installed) it doesn't work because the DLL is not registered.  It there another method of accomplishing this task?  OpenXML?

Here is code I have worked with thus far:

        public string convertWordToText(string fileName)
        {
            try
            {
                object missing = Type.Missing;
                object readOnly = true;

                Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document document = application.Documents.Open(fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                string text = document.Content.Text;
                ((Microsoft.Office.Interop.Word._Application)application).Quit(); //cast as _Application because there's ambiguity
                object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                ((Microsoft.Office.Interop.Word._Application)application).Quit(ref saveChanges, ref missing, ref missing); //cast as _Application because there's ambiguity
                return text;
            }
            catch (Exception ex)
            {
                Log.error("Error converting document to text.");
                return string.Format("An error occured when converting this document to text.  The error returned is '{0}'.  \n\nYou can try copy and paste instead.", ex.Message);
            }
        }
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE