0

I am using Microsoft.Office.Interop, for scanning word and excel files using C#,I am taking file as input from user, saving it on local directory then scanning it Interop library. Its working fine on my local publish, However when I published it on server its not scanning Word or Excel Files. I have MS-Office installed on my Server, the same version which I have on my PC. What else I need to do? I have copied the required DLL's in my bin Directory. This is how I am reading word file

public static string word_to_text(string source)
{
  Word.Application newApp = new Word.Application();

  object Source = source;
  object Unknown = Type.Missing;

  object miss = System.Reflection.Missing.Value;
  object readOnly = true;
  Microsoft.Office.Interop.Word.Document docs = newApp.Documents.Open(ref Source, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
  string totaltext = "";
  for (int j = 0; j < docs.Paragraphs.Count; j++)
  {
    totaltext += " \r\n " + docs.Paragraphs[j + 1].Range.Text.ToString();
  }
  docs.Close();
  newApp.Quit();
  return totaltext;
}

I was getting this exception

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

I fixed it by following this post However now I am having issue with excel and word files. In case of Word files,its not reading the file from above code,docs object is appearing to be null. In case of Excel Its showing exception

Microsoft Office Excel cannot access the file 'myfile path'. There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook.

I read solution to give rights to folder, and also create Desktop folder inside system32 Here,both solutions not worked for me :( The issue is arising only when I am publishing it

Community
  • 1
  • 1
Syed Salman Raza Zaidi
  • 2,172
  • 9
  • 42
  • 87

1 Answers1

1

You should not use Office automation in a server. Only one thread can access Word at a time, it's very slow relative to other options, and Word is not made for server-side execution.

Much better to use OpenXML SDK:

http://www.microsoft.com/en-us/download/details.aspx?id=5124

Or some other wrapper:

http://docx.codeplex.com/

http://www.codeproject.com/Articles/87711/Manipulate-Docx-with-C-without-Microsoft-Word-inst

If you feel you absolutely must use Office automation on a server (and you shouldn't), then at least put it in a separate process and enque requests to the other process. That way you can ensure only one request is being processed at a time, you can safely grant the other process the permissions needed, and if Word halts you can kill it and the secondary process.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • @SyedSalmanRazaZaidi, I see your updates but I still stand behind my strong recommendation that you should never use Office automation on a server. Use OpenXML SDK instead. – Samuel Neff Aug 27 '13 at 00:38