getting error for opening MS word doc in IIS 7,

3.2k Views Asked by At

I'm developing some pages by Asp.Net which contains opening and reading some MS word docs, it works well by my VS 2010 on its own localhost but when I put the files under wwwroot and run the pages by Inetmgr I get this error :

This command is not available because no document is open.

where do you think the problem is? shall I add some references or edit some setting in my IIS manager to work with MS word docs? how? thanks :)

and this is how I'm trying to open and read a doc called stopwords.doc

static extractor()
    {
        try
        {
            ApplicationClass wordApp = new ApplicationClass();
            string[] words;
            char[] delimiterChars = { ' ', ',', '.', '\r', '\t', '\n' };
            string filePath =   "http://localhost:8777/stopwords.doc";
            object file = filePath;
            object nullobj = System.Reflection.Missing.Value;                
            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref file,
            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
            ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
            ref nullobj);
            Microsoft.Office.Interop.Word.Document doc1 = wordApp.ActiveDocument;
            string m_Content = doc1.Content.Text;
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            if (m_Content != null)
            {
                words = m_Content.Split(delimiterChars);
                foreach (string s in words)
                {
                    if (!stopwords.ContainsKey(s))
                        stopwords.Add(s, s);
                }
            }

        }
        catch (Exception)
        {
            throw;
        }

    }
2

There are 2 best solutions below

3
On BEST ANSWER

Typically the accounts running web processes don't have enough os permissions to run GUI apps like Word. This is probably a good thing in most cases as one really shouldn't run GUI apps from web processes. What happens when word springs a modal dialog that blocks the entire site and no one is available to visit the web server and click OK. If you must read word docs, a library is a much better solution.

All that said, using word here makes zero sense -- you are just loading some stop words from a file. Plain text would be just as effective and vastly easier to deal with.

1
On
string filePath =   "http://localhost:8777/stopwords.doc";

this is not a valid filepath.

try to put the file inside the web application folder, for example create a folder called WordDocs inside the place where you deployed your ASP.NET application then get the file path like this:

string filePath = Server.MapthPath("~/WordDocs/stopwords.doc");

if(File.Exists(filePath))
{
  object file = filePath;
  object nullobj = System.Reflection.Missing.Value;
  ...
}