0
votes

I want to read text from a Word file. Its working properly on localhost but when I run it online then I get this error:

Could not load file or assembly 'office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.

My code:

Application Objword = new Application();
Document objdoc = new Document();
dynamic FilePath = fileLoc;
dynamic NA = System.Type.Missing;
objdoc = Objword.Documents.Open(ref FilePath, ref NA, ref NA, ref NA, ref NA,ref NA, ref NA, ref NA, ref NA,
        ref NA, ref NA, ref NA, ref NA,ref NA, ref NA, ref NA);
StringBuilder sb = new StringBuilder();
for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)
{
    string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();

    if (Filedata != string.Empty)
    {
        //Append word files data to stringbuilder
        sb.AppendLine(Filedata);
    }
}
((_Document)objdoc).Close();
((_Application)Objword).Quit();
TextBox1.Text = Convert.ToString(sb);

I have also placed dll file in bin folder with same version but still getting error.

1
Is the office dll the correct versionMarcus Höglund
yes correct version. Its working properly on localhost.Sukhvindra Singh
Using Microsoft.Office.Interop in a server environment is not recommended (since it is very prone to errors) and also is not supported by Microsoft (see this link: Considerations for server-side Automation of Office). You should probably look into using some kind of library like OpenXML to manipulate the office files on the server.bassfader

1 Answers

0
votes

It seems to be a very peculiar issue with running office on a server. Suggestions are you would need to install the same version of office on your server as that in your development machine. Because, apart from the interop dll, you would also require more than just the DLL.

Additionally, step through your code and check where it fails, probably, the interop is returning an error somewhere in your code and is returning another error.

For example, if the error message is in the first line, then it is related to the DLL file, however, if it fails somewhere down, then that call is giving the error and not the actual loading of the DLL.