Reputation: 247
I have a function I use to save a word doc as a html file, as part of a web-based document viewer. It returns a path to the newly created html file, which is then displayed in an iframe. I've pasted its contents below for reference.
This all works fine in when I'm running the web application from visual studio, but when it's installed on IIS it just hangs. The html file is not created.
Previous to this situation, the function would only get as far as instantiating the word Application class; I was receiving an error along the lines of:
"Retrieving the COM class factory for component {Guid-thing} failed due to the following error..."
That was solved by going to Component Services --> Computers --> My Computer --> DCOM Config. I went to the security properties of the Microsoft Word 97 - 2003 Docment and gave the permissions Local Launch and Local Activation to the IIS user / Network Service. Later, I also gave Remote Launch and Remote Activation permissions.
When I try to view a document, I see in task manager, under processes, that WINWORD.exe has been opened by Network Service. So it's getting further than it did previously. But now it seems to me that it is either hanging on the wordApp.Documents.Open or doc.SaveAs lines. I've left it chugging along for up to ten minutes at points, but no timeout is ever hit, or error displayed.
The web application is installed in IIS 6, on Windows Server 2003 SP2
private string WordToHTML(string filePath)
{
string returnPath = String.Empty;
Microsoft.Office.Interop.Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = null;
try
{
// Initialise
string serverFolderPath = Path.GetDirectoryName(filePath);
string fileToOpen = filePath;
string FileToSave = Path.GetFileNameWithoutExtension(fileToOpen) + ".html";
// Open Word
wordApp = new Microsoft.Office.Interop.Word.Application();
// Get Doc
doc = wordApp.Documents.Open(fileToOpen, ref missing, true, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, false, ref missing, ref missing,
ref missing, ref missing);
// If html file already exists, delete it
returnPath = String.Format("{0}\\{1}", serverFolderPath, FileToSave);
if (File.Exists(returnPath))
File.Delete(returnPath);
// Save as HTML document
doc.SaveAs(returnPath, 10, 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, ref missing);
}
catch (Exception ex)
{
throw ex;
}
finally
{
doc.Close(ref missing, ref missing, ref missing);
wordApp.Quit(ref missing, ref missing, ref missing);
}
return returnPath;
}
Upvotes: 1
Views: 1082
Reputation: 1319
Using any MS Office product from within code is extremely prone to this sort of issue, and (tough as the advise might appear) you really should avoid doing what you are doing here at all costs.
You are most likely suffering from a "silent ui prompt". There should be a UI prompt on screen to respond to, but due to the execution context/and the fact that you are not logged onto the machine in question, it never appears.
Other areas of gotcha when working with MS Office stuff in this form include when you dispose of objects. If you don't get this 100% perfect, then you will have a massive memory leak in your application that will continually bring it down over time. I found that if you write your code never chain calls together, and always assign each object to a variable before working with it, that this can be slightly easier to achieve (as you can dispose of every object specifically
You should also look at upgrading the document format from 97/2003 to the xml formats of Office 2007 onwards. The file size is significantly better, and it is more compatible with recent products that might web view it for you.
You should ask whether the costs of doing this piece of work (correctly and reliably) will be cheaper than simply using Microsoft's own "Office Web Apps", which you can access via SharePoint within the enterprise, or through Office 365, which is competitively costed and gives you a hell of a lot more that "just perfect web viewing".
Upvotes: 1