I am trying to add a file on a word document
application.Selection.InsertFile(file);
But it causes the exception
COMException: The document name or path is not valid
And then save it
document.SaveAs(path);
But then I get the exception
The object invoked has disconnected from its clients. (Exception HRESULT: 0x80010108 (RPC_E_DISCONNECTED))
The exact same code works when running from Visual Studio. The exception only happens when I try to run from IIS.
Could it be related to some permission? I have given full control permissions to Everyone
, ISUSR
, Network
, Network Service
, System
, Administrators
... And I also tried to use Impersonate with an administrator account.
How can I fix it so it will work when I run from IIS?
Here is the class I made to manipulate the doc: http://pastebin.com/yB2s0jn4
And here is how I am calling it
using (var doc = new HtmlWordDocument(outFile))
{
// calls Selection.InsertFile( file )
doc.WriteContent(tempFile);
// calls document.SaveAs()
doc.Save();
}
Have you tried impersonation (How to implement impersonation in an ASP.NET application)? like below:
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
using (var doc = new HtmlWordDocument(outFile))
{
// calls Selection.InsertFile( file )
doc.WriteContent(tempFile);
// calls document.SaveAs()
doc.Save();
}
impersonationContext.Undo();
you need IISADMIN service enabled but it's not istalled by default, this service will permit to connect IIS to more OS resources
Check this...
and this...
User contributions licensed under CC BY-SA 3.0