Solved

"Access is denied" when attempting to open MS Word from COM+ Application running as "Local Service"

Posted on 2008-06-13
2
707 Views
Last Modified: 2012-08-13
I have been requested to make a change to the above COM+ application to allow the MS Word Documents it processes to be saved as Text. I wrote a test application and sucessfully converted the Word Document to DOS Text so I am confident that it is not the application that is causing the issue.

When the application runs on the server my Trace log tells me that the last attempted operation was Word.Application myWord = new Word.Application(); and I can see the WinWord.exe pop up in Task Manager.
The next trace line is the error : "ERROR : Failed to open Word. Error : Access is denied.".

I am told that the application is impersonating the local Adminstrator log in so it should have full access to the system. I have tried giving "Full Control" to all users for the "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE" but this has made no difference.

Does anyone know why my application can open Word yet have no access to it ? Or perhaps there something that Word is trying to use that it doesn't have the necessary access for ?

Any help and/or assistance would be greatly appreciated.


Yours, Matthew.

using Word = Microsoft.Office.Interop.Word;
 

private bool convertWordDoc(string WordDocName, eWdSaveFormat SaveAsFormat, string FileExtension, out string NewDocName, out string ErrorText)

{

	Trace("Starting Word Document Conversion to " + SaveAsFormat.ToString());
 

	//get the name and location of the source file

	FileInfo fileName = new FileInfo(WordDocName);

	Trace("Doc Name     : " + fileName.Name);

	Trace("Doc Location : " + fileName.DirectoryName);
 

	//default the output new name to that of the original

	NewDocName = WordDocName;
 

	Trace("Return Doc Name set to : " + NewDocName);

	//Assuming that ".doc" is a Word Document, then if this is not a ".doc" then we can't use Word to reformat it

	if (fileName.Extension != ".doc" )

	{

		ErrorText  = "The document being sent is not a Word document and cannot be reformatted with Word.";

		Trace("ERROR : Not a Word Doc, outputting New Document Name");

		return false;

	}

	

	Word.Application myWord = null;

	Word.Document myDoc = null;
 

	ErrorText  = "";

	object Source = WordDocName;

	object Unknown = Type.Missing;
 

	//pass the new name out

	NewDocName = new FileInfo(fileName.FullName.Substring(0, (fileName.FullName.Length - fileName.Extension.Length) + 1) + FileExtension).ToString();

	Trace("Return Doc Name re-set to : " + NewDocName);
 

	try

	{

		Trace("Opening Word");

		myWord = new Word.Application();		//<-- Error Occurs here

		Trace("Word is open");

	}

	catch(Exception e)

	{

		Trace("ERROR : Failed to open Word. Error : " + e.Message + Environment.NewLine + e.Source + Environment.NewLine + e.StackTrace);

		//dispose of the open objects

		myDoc = null;

		myWord = null;
 

		//log an error here

		ErrorText = e.ToString();

		return false;

	}
 

	try

	{

		myDoc = myWord.Documents.Open(ref Source, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

		Trace("Document open in Word");

	}

	catch(Exception e)

	{

		Trace("ERROR : Failed to open the Document in Word. Error : " + e.Message + Environment.NewLine + e.Source + Environment.NewLine + e.StackTrace);

		//dispose of the open objects

		myDoc.Close(ref Unknown, ref Unknown, ref Unknown);

		myWord.Quit(ref Unknown, ref Unknown, ref Unknown);

		myDoc = null;

		myWord = null;
 

		//log an error here

		ErrorText = e.ToString();

		return false;

	}
 

	// Specifying the format in which you want the output file 

	object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;

	switch(SaveAsFormat)

	{

		case eWdSaveFormat.wdFormatDOSText:											// Microsoft DOS text format. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDOSText;

			break;

		case eWdSaveFormat.wdFormatDOSTextLineBreaks:								// Microsoft DOS text with line breaks preserved. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDOSTextLineBreaks;

			break;

		case eWdSaveFormat.wdFormatEncodedText:										// Encoded text format. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatEncodedText;

			break;

		case eWdSaveFormat.wdFormatFilteredHTML:									// Filtered HTML format. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML;

			break;

		case eWdSaveFormat.wdFormatHTML:											// Standard HTML format. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;

			break;

		case eWdSaveFormat.wdFormatRTF:												// Rich text format (RTF). 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;

			break;

		case eWdSaveFormat.wdFormatTemplate:										// Microsoft Word template format. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatTemplate;

			break;

		case eWdSaveFormat.wdFormatText:											// Microsoft Windows text format. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;

			break;

		case eWdSaveFormat.wdFormatTextLineBreaks:									// Microsoft Windows text format with line breaks preserved. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatTextLineBreaks;

			break;

		case eWdSaveFormat.wdFormatUnicodeText:										// Unicode text format. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatUnicodeText;

			break;

		case eWdSaveFormat.wdFormatWebArchive:										// Web archive format. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatWebArchive;

			break;

		case eWdSaveFormat.wdFormatXML:												// Extensible Markup Language (XML) format. 

			format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML;

			break;

	}

	Trace("Saving format set to : " + format.ToString());
 

	//pass the new name in to the Word Save As bit

	object Target = new FileInfo(fileName.FullName.Substring(0, (fileName.FullName.Length - fileName.Extension.Length) + 1) + FileExtension).ToString();

	Trace("Target Doc Name set to : " + Target);

	try

	{

		//Changing the format of the document

		Trace("Saving the Document");

		myDoc.SaveAs(ref Target, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

		Trace("Document saved to Target : " + Target.ToString());

	}

	catch(Exception e)

	{

		Trace("ERROR : Failed to save Target to new format. Error : " + e.Message + Environment.NewLine + e.Source + Environment.NewLine + e.StackTrace);

		//log an error here

		ErrorText = e.ToString();

		return false;

	}

	finally

	{				

		//dispose of the open objects

		myDoc.Close(ref Unknown, ref Unknown, ref Unknown);

		myWord.Quit(ref Unknown, ref Unknown, ref Unknown);

		myDoc = null;

		myWord = null;

	}
 

	Trace("Returning success, outputting New Document Name");

	return true;

}
 

public static void Trace(string Message)

{

	TextWriter txtWriter = null;

	try

	{

		txtWriter = new StreamWriter(@"F:\Vows Logs\" + DateTime.Now.ToString("yyyy MM dd") + " VOWS Server.log", true);

		try

		{

			txtWriter.WriteLine(DateTime.Now.ToString("yyyy MM dd HH:mm:ss") + " " + Message);

		}

		catch //catch everything

		{

			//ignore failure to log errors

		}

		finally

		{

			txtWriter.Flush();

			txtWriter.Close();

		}

	}

	catch //catch everything

	{

		//ignore failure to log errors

	}

	finally

	{

		txtWriter = null;

	}

}
 

######## Trace Output ########
 

2008 06 12 16:02:42 Starting Conversion

2008 06 12 16:02:43 Doc Name     : NurseNUR23-1-lettertoGoAheadPensionGroup_1493017.doc

2008 06 12 16:02:43 Doc Location : f:\vows\_Temp

2008 06 12 16:02:43 Return Doc Name set to : f:\vows\_Temp\NurseNUR23-1-lettertoGoAheadPensionGroup_1493017.doc

2008 06 12 16:02:43 Return Doc Name re-set to : f:\vows\_Temp\NurseNUR23-1-lettertoGoAheadPensionGroup_1493017.drf

2008 06 12 16:02:43 Opening Word

2008 06 12 16:03:23 ERROR : Failed to open Word. Error : Access is denied.

VOWSServer

   at VOWS.Server.DocSC.convertWordDoc(String WordDocName, eWdSaveFormat SaveAsFormat, String FileExtension, String& NewDocName, String& ErrorText)

Open in new window

0
Comment
Question by:Voicepath
2 Comments
 
LVL 7

Accepted Solution

by:
alexpercsi earned 250 total points
ID: 21783481
There are two possibilities that i know of:

1. You may need to configure your Word COM component. To do this go to start-run-dcomcnfg. Find the Word Com component and configure it.

2. Impersonation may not grant sufficient rights for ASP.NET to launch an application. Try adding the user account it runs under (i.e.) NETWORK SERVICE or ASPNET to the Administrators group. I know, this may reduce your security, but it may not work otherwise.
0
 

Author Comment

by:Voicepath
ID: 21792352
Hi alexpercsi

Thank you for your suggestion.

Firstly, you didn't clarify what to configure the Word DCOM to ! I assume you meant that I should set the access permission on the security tab to a "Custom Setting" rather than the "Use Default".

Secondly, since posting this question my management have changed the specification (again !). It transpired that MS Word is not installed on the server the system will be made live on. So, I can't use MS Word to do the job I wanted. Instead I am reading the Word Document as a stream, locating the bit I need and writing that bit out with a TextWriter.

Thanks again for taking the time to respond.

I will accept your solution so you can take the points.


Yours, Matthew.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now