Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good day all
I have a bit of a head scratcher on my hands.
The following code runs on my server and it does work for what it is intended.

C#
public void RenderWithData(string strcaseno, string strdocpath, string strdocsp, string stramnt)
{
	Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application[];
	string suffix = Convert.ToString(DateTime.Now.Minute + DateTime.Now.Millisecond);
	string sourceFileName = System.Web.HttpContext.Current.Server.MapPath(strdocpath);
	string destFileName = System.Web.HttpContext.Current.Server.MapPath("~/Cache/" + ActiveLogin.Login + Session.SessionID.ToString + suffix + ".doc");
	Word.Document docDepetal = new Word.Document();
	FileInfo objFileInfo = default(FileInfo);

	try {
		File.Copy(sourceFileName, destFileName);

		SqlSingleQuery cmd = new SqlSingleQuery(strdocsp);
		cmd.AddInt("@USERID", ActiveLogin.UserID);
		string ParameterName = "value0";
		cmd.AddVarChar(ParameterName, 50, strcaseno);
		cmd.AddMoney("@NEWCONSENT", stramnt);
		cmd.Execute();

		docDepetal = appWord.Documents.Open(destFileName);

		Word.Bookmarks MyBookMarks = docDepetal.Bookmarks();


		foreach (string bookmark in cmd.Columns.Keys) {
			MyBookMarks.Item(bookmark).Range.Text = cmd.Columns.Item(bookmark).ToString();
		}


		docDepetal.Protect(Word.WdProtectionType.wdAllowOnlyComments, false, "password");
		docDepetal.Save();
		docDepetal.Close();
		appWord.Quit();
		Marshal.FinalReleaseComObject(appWord);
		appWord = null;
		objFileInfo = new FileInfo(destFileName);
		DisplayDownloadDialog(objFileInfo);

	} catch (Exception ex) {
		ShowErrorMsg(ex.Message);
	} finally {
		if (appWord != null) {
			if (docDepetal != null) {
				docDepetal.Close();
			}
			appWord.Quit();
			Marshal.FinalReleaseComObject(appWord);
		}

		if (File.Exists(destFileName)) {
			File.Delete(destFileName);
		}
	}
}


Now my problem is every once in a while the winword.exe process will not close on the server, the every other winword.exe process that opens after that will not close either. This then results in a "creating an instance of the com component with clsid {00020906-0000-0000-c000-000000000046} from the iclassfactory failed due to the following error: 8001010a." error that is displayed each time a new request is sent to create a document.

I would like to know if there is something I can do differently in this code that would resolve this issue.

Any help is greatly appreciated and thank you in advance.
Posted

Running Word on a server is just a very bad idea. It is an interactive program that can for example hang on the server because it shows a message for example if you are sure you want to close the document without saving. Because there is no interactive user on the server, nobody is responding to that message and word just keeps waiting for until somebody does. There are alternative components that would be better to use on a server.

http://support.microsoft.com/kb/257757/en-us[^]

Good luck!
 
Share this answer
 
v2
Comments
BulletVictim 12-Nov-13 4:17am    
This does not physically open the word application on the server. This just opens the process in the task manager and should close it in the task manager again. The documents being created are templates that are being populated at run time. so no interaction is needed from a user.

Please could you remove this answer, so that this question could go back into the unanswered list.
E.F. Nijboer 12-Nov-13 5:10am    
I understand that there is no need for interaction. Except, the problem is that Word IS an interactive application. That's why you only see the process in the task manager but don't get to see a messagebox requesting for actual user interaction. As microsoft states on their own website, it is developed and tested to be an end-user program to run on a client workstation. I added a link about it to the answer.
I know it might be a very inconvenient answer, but I'm sorry to say it is the actual answer nonetheless.
sometimes a component has some problem. Try re-installing the particular component might fix it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900