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();
}