"Access is denied"---about word automation

R

Ron

Hi there,

We have a web application written in C# that need to deal with MS word
2003. it works fine on developer's machine but when I deployed it on
web server, we got error msg says "Access is denied". So i guess the
code is OK but web server configuraion has to be changed.

I got .Net framework 1.1 installed on webserver and deployed the
webapplication. Then i installed Office 2003. and set DCOM(word)
permission and web folder access permission to ASPNET account.

When i run the application, I got error msg says "Access is denied".
Can anybody help to solve the problem?
Thanks in advance,

Cheers,
Ron
 
Joined
Aug 4, 2006
Messages
1
Reaction score
0
Office Automation using C#

Hi,

We are trying to convert word and excel to html format, we are using late binding to support different version of office. We have already taken care of setting dcom security to interactive user and also we have given 'launch and active' and 'Access permissions' for aspnet(Network service in case of 2003) user.

The problem is it works absolutely fine in some systems, but it will not work in some systems and throws error as Unauthorizedaccess for CreateObject call.

The only difference i could see in working and non working system is, in working system dcom services of excel and word are running with name like "Microsoft Excel Application", but in nonworking system these servies are runnig with GUID like "00020812-0000-0000-C000-000000000046", and this is just a difference i saw I am really not sure whether problem is because of this only or not.

And also folder permission has given for aspnet user(network service for windows 2003) which contains the word and excel docs to be converted to html

PS: We are no supposed to impersonate aspnet user, so in our web confgi we set impersoantion to false.

Please help me in this i am really trying hard to find solution for this.

Following is the code used.
if(wdFileName.ToString().Substring(wdFileName.ToString().LastIndexOf(".") + 1).ToString().ToUpper() != "DOC")
throw new Exception(RM.GetString("OfficeSnippet.InvalidWordFileType"));

//This absolute filename is required for saving the file as html.
string AbsPath = Server.MapPath(SnippetCommon.DefaultFolders.OfficeFolder) + @"\ShiftSummary\Snippet\Word\" + wdFileName.ToString();
if(!System.IO.File.Exists(AbsPath))
{
LogMessage(ODPriority.Error, RM.GetString("OfficeSnippet.FileNotFound"));
this.ShowOutput(lblOutput,RM.GetString("OfficeSnippet.FileNotFound"),"",1);
return;
}
// Get rid of the .xsl extension and then appended the html extension
object FileNameSaveAs = AbsPath.ToString().Substring(0,AbsPath.ToString().LastIndexOf(".")) + ".html";
//The virtual path of the file is required for viewing the html file.
string VPath = SnippetCommon.DefaultFolders.OfficeFolder + @"\ShiftSummary\Snippet\Word\" + FileNameSaveAs.ToString().Substring(FileNameSaveAs.ToString().LastIndexOf("\\") + 1);

object WordApp = new object();
object wrdDoc = new object();
object WordDocs = new object();
object [] oParamDocPath = null;
object [] oParamHtmlPath = null;

try
{
System.Type WordType = Type.GetTypeFromProgID("Word.Application");
WordApp = Activator.CreateInstance(WordType,true);

string Version = Convert.ToString(WordApp.GetType().InvokeMember("Version",System.Reflection.BindingFlags.GetProperty,null,WordApp,null));

int WordhtmlFormat = 8;
if (Version.StartsWith("9.") || Version.StartsWith("10."))
{
//Office 2000, 12 params
//Reference: http://msdn.microsoft.com/library/en-us/off2000/html/womthopen.asp?frame=true
oParamDocPath = new object[12];
oParamDocPath[0] = AbsPath;
oParamDocPath[1] = Type.Missing;
oParamDocPath[2] = true;
oParamDocPath[3] = Type.Missing;
oParamDocPath[4] = Type.Missing;
oParamDocPath[5] = Type.Missing;
oParamDocPath[6] = Type.Missing;
oParamDocPath[7] = Type.Missing;
oParamDocPath[8] = Type.Missing;
oParamDocPath[9] = Type.Missing;
oParamDocPath[10] = Type.Missing;
oParamDocPath[11] = Type.Missing;

//Save As supports 11 parameters
oParamHtmlPath = new object[11];
oParamHtmlPath[0] = FileNameSaveAs;
oParamHtmlPath[1] = WordhtmlFormat;
oParamHtmlPath[2] = Type.Missing;
oParamHtmlPath[3] = Type.Missing;
oParamHtmlPath[4] = Type.Missing;
oParamHtmlPath[5] = Type.Missing;
oParamHtmlPath[6] = true; //ReadOnlyRecommended
oParamHtmlPath[7] = Type.Missing;
oParamHtmlPath[8] = Type.Missing;
oParamHtmlPath[9] = Type.Missing;
oParamHtmlPath[10] = Type.Missing;

}
else if(Version.StartsWith("11."))
{
//orginal code

oParamDocPath = new object[1];
oParamHtmlPath = new object[2];
oParamDocPath[0] = AbsPath;
oParamHtmlPath[0] = FileNameSaveAs;
oParamHtmlPath[1] = WordhtmlFormat;

}
else
{
throw new Exception("The version " + Version + " of word is not supported. Please install word 2000 (v9.0), 2002(v10.0), or 2003(v11.0)");
}


WordDocs = WordType.InvokeMember("Documents",System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public ,null,WordApp,null);
wrdDoc = WordDocs.GetType().InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public,null,WordDocs,oParamDocPath);
wrdDoc.GetType().InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public,null,wrdDoc,oParamHtmlPath);
WordApp.GetType().InvokeMember("Quit",System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public,null,WordApp,null);
if(this.docFrame != null)
this.docFrame.Attributes.Add("src",VPath.ToString());

}
catch (System.Reflection.TargetInvocationException exp)
{
this.ShowOutput(lblOutput,RM.GetString("OfficeSnippet.Word_Not_Install"),exp.Message,1);
this.LogMessage(ODPriority.Error,exp.GetBaseException().ToString());
}
catch(Exception exp)
{
this.ShowOutput(lblOutput,RM.GetString("OfficeSnippet.GenericError"),"",1);
this.LogMessage(ODPriority.Error,exp.GetBaseException().ToString());
}
finally
{
NAR(WordDocs);
NAR(wrdDoc);
NAR(WordApp);
GC.Collect();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top