Solved

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

Posted on 2008-06-13
2
724 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why does my excel data start at B1 instead of A1. 9 84
SqlDependency to get update from sql to my c# app 2 40
Cant save 3D 4 20
C# Gridview 1 32
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…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

778 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