Reputation: 590
I am currently retrieving a word file that's on a file system to display it in a MVC 5 razer view in .NET. Now, the file is properly being displayed on my local IIS when testing and using my local machine. I also keep the file on my local machine for testing purposes.
Everything works fine when testing on my local machine. I then publish/deploy the changes to my remote server's IIS and check to see if the site is working. When I go to the page where the Word file is displayed, I get an error message "An error occurred while processing your request."
The code is below
public ActionResult GetChapterContent()
{
StringBuilder text = new StringBuilder();
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
object path = @"/Books/1/Chapters/1/chapter-1.docx";
//object path = @"/Books/1/Chapters/1/chapter-1.docx";
object readOnly = true;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, 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);
var y = false;
for (int i = 0; i < docs.Paragraphs.Count; i++)
{
if(docs.Paragraphs[i + 1].Range.Text.Contains("***")) {
if (y == false)
{
text.Append("<span class='ci'>" + docs.Paragraphs[i + 1].Range.Text.ToString().Substring(3));
y = true;
}
else
{
text.Append(docs.Paragraphs[i + 1].Range.Text.ToString().Remove(docs.Paragraphs[i + 1].Range.Text.ToString().Length - 4, 3) + "</span>");
y = false;
}
}
else {
text.Append("" + docs.Paragraphs[i + 1].Range.Text.ToString());
}
}
return Content("DF");
}
Now, I've managed to narrow down the issue to just the first two lines of code:
StringBuilder text = new StringBuilder();
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Just having these two lines of code will result in the error. If I remove it - all will be fine.
Upvotes: 0
Views: 633
Reputation: 10929
Two things here because Microsoft.Office.Interop
will require a valid office pack on your system.
1) use NPOI
It will not require a valid office pack on the system and you can use it for read write etc...
2) use OpenXml
For word processing, style-sheet and cell processing.
Each of them will get to the desired location the question is, what will suite your needs best.
third option is to install office pack on the remote server.
Upvotes: 1
Reputation: 43
Maybe you can try NPOI,it can operate your excel and word without office.
Upvotes: 1