0

Iam facing issue while reading the document when runnning code from IIS. It is not reading the document and throwirng an error "No document is opne when trying to save the activedocument.".

 m_word.Documents.Open(ref FileName,
                                ref missing, ref readOnly, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);


 m_word.ActiveDocument.SaveAs2(ref FileName_rtf, ref FileFormat, ref missing1, ref missing1,
                    ref missing1, ref missing1, ref missing1, ref missing1,
                    ref missing1, ref missing1, ref missing1,
                    ref missing1, ref missing1, ref missing1, ref missing1, ref missing1, ref missing1);

m_word.ActiveDocument is giving error when running from IIS. It is working in visual studio debugging mode.

I also tried the below code:

 Document doc_open =  m_word.Documents.Open(ref FileName,
                                ref missing, ref readOnly, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            doc_open.Activate();

doc_open is retruning null and it throws object reference error in the second line. this works when code is in debugging mode. Error occurs when ran from IIS on development server where we have IIS 7 and MS offices too.

Kara
  • 6,115
  • 16
  • 50
  • 57

3 Answers3

1

Add: C:\Windows\SysWOW64\config\systemprofile\Desktop

It's incredibly successful.

Scott
  • 21,211
  • 8
  • 65
  • 72
DreamZhang
  • 11
  • 1
0

could be due to security setting, check this http://support.microsoft.com/kb/257757/en-us. Not a good idea to use interop on server, you may look at other option like openxml sdk

Brijesh Mishra
  • 2,738
  • 1
  • 21
  • 36
  • I created a service account and added the same to IIS application pool and also gave admin rights to that service account. Still the error occus. SO this might not be user specific security issue. Still no clue.. Please help. – Sudeep vajrala Jan 03 '12 at 07:28
  • can you debug and find out if you are able to get handle to m_word? – Brijesh Mishra Jan 03 '12 at 07:34
  • I am getting m_word as null when debugged via IIS. – Sudeep vajrala Jan 03 '12 at 08:57
  • ok so thats the problem, how are you getting value for m_word – Brijesh Mishra Jan 03 '12 at 09:05
  • I am sorry Iam getting Document doc_open as null not the m_word. m_word is not null and is an instance of Application class. when I use m_word.open from IIS no document is opened. And the m_word.Acitvedocument throws error. "No Document is open" – Sudeep vajrala Jan 03 '12 at 09:25
  • can you prite FileName and chack if its correct? and check if your web app has read/write IO permission? – Brijesh Mishra Jan 03 '12 at 09:30
  • Just to clarify the access right on the file I wrote some code to read the file i tried to upload in the above code and it worked. Iam able to read the file and copy it as html file. But this does not serve my requiredment as I need to read the word as document Thanks for your support – Sudeep vajrala Jan 03 '12 at 09:39
  • object. string _PostedFile = Server.MapPath("FinalWordDcoument.docx"); string FilePath = Server.MapPath("FinalWordDcoument.html"); File.Copy(_PostedFile, FilePath); Followin is the exact Iam getting when tried to access m_word.ACtivedocument "" string _PostedFile = Server.MapPath("FinalWordDcoument.docx"); string FilePath = Server.MapPath("FinalWordDcoument.html"); File.Copy(_PostedFile, FilePath);"" – Sudeep vajrala Jan 03 '12 at 09:39
  • write a valnila test function to open fie like Document doc = m_word.Documents.Open( @"C:\FinalWordDoc.docx", ReadOnly: false, Visible: false ); and see if it works, – Brijesh Mishra Jan 03 '12 at 09:52
  • I tried the belwo code object FileName = @"f:\FinalWordDoc.docx"; object readOnly = true; object missing = Type.Missing; Document doc = m_word.Documents.Open(ref FileName,ref missing, ref readOnly, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.Activate(); //Erro: Object reference not set to an instance of an object m_word.ActiveDocument.Close(ref save, ref missing, ref missing);//error This command is not available because no document is open – Sudeep vajrala Jan 03 '12 at 10:24
  • maybe this will help http://www.lokkju.com/blog/archives/73, http://forums.asp.net/t/1328690.aspx – Brijesh Mishra Jan 03 '12 at 16:10
  • Thanks for your support. Iam still facing the same issue. "This command is not available because no document is open." I have MS office 2010 Installed and IIS7 on the server. – Sudeep vajrala Jan 04 '12 at 05:20
  • I tried adding the folder: C:\Windows\SysWOW64\config\systemprofile\Desktop and Voila! It's now working. But one issue is with the IIS setting. I have changed the Application pool Identity to Local System inorder to get it worked But It should be Network Service. When I use NEtwork Service it is giving the following error. Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005. Please help me on this. – Sudeep vajrala Jan 04 '12 at 06:50
  • assign rigt priviledge to NTservice account or run interop code under impersonationcontext http://justgeeks.blogspot.com/2007/09/impersonating-user-programmatically-in_06.html – Brijesh Mishra Jan 04 '12 at 07:10
  • I tried impersonation and when deployed on iis the page is loading for infinite time. I ended the process to stop it. It is working in debugging mode but when run from iis the page is hanged. I also tried adding privilage for NEtwork service to the COMponent with above mentioned CLSID. Still no luck. – Sudeep vajrala Jan 04 '12 at 08:58
  • use Debugger.Break to attach process and check where it hangs – Brijesh Mishra Jan 04 '12 at 09:12
  • Thanks for your quick responses. It is hanging while checking the impersonation. Do i need to change any thing in the config. – Sudeep vajrala Jan 04 '12 at 09:42
  • Now after using the belwo code iam getting error " while openmieng the word . System.Security.Principal.WindowsImpersonationContext impersonationContext1; impersonationContext1 = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();Word could not create the work file. Check the temp environment variable." – Sudeep vajrala Jan 04 '12 at 10:36
0

Don't do this, it's not officially supported (highlighting by me):

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

Source: MS KB 257757. The article also contains a list of alternative solutions. If all else fails, you might have to resort to a third-party library that understands the Word format.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Actually my requirement is to read the document or rtf file and convert the same as html and give the HTML to the Rad editor. Which will help me to import the text images and styles from word document to the RAd editor. So iam trying to read the document and save it as html and them copy the converted html to radeditor content area. Please let me know is there any other way to do this. Iam using older version of RAd editor so idont have LoadRTFContent method. – Sudeep vajrala Jan 03 '12 at 09:49
  • I did a quick Google search -- there seem to be a few commercial Word-to-HTML-converters which support ASP.NET out there. – Heinzi Jan 03 '12 at 14:12
  • I tried adding the folder: C:\Windows\SysWOW64\config\systemprofile\Desktop and Voila! It's now working. But one issue is with the IIS setting. I have changed the Application pool Identity to Local System inorder to get it worked But It should be Network Service. When I use NEtwork Service it is giving the following error. Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005. Please help me on this. – Sudeep vajrala Jan 04 '12 at 06:50
  • What is the disadvantage of having Local system set in app pool instead of NEtwork service? – Sudeep vajrala Jan 04 '12 at 11:55
  • 1
    Finally able to resolve using service account in the app pool and add the same account in the admin group. Thanks a lot for your support – Sudeep vajrala Jan 04 '12 at 13:39
  • @Sudeepvajrala: Thanks for sharing your solution. Please add it as an answer to your question and "accept" your answer, so that people reading this question will find your solution easily. – Heinzi Jan 04 '12 at 13:51