I want to open a word file saved in my server using “Microsoft.Office.Interop.Word”.
This is my code:
object missing = System.Reflection.Missing.Value; object readOnly = false; object isVisible = true; object fileName = "http://localhost:52099/modelloBusta/prova.dotx"; Microsoft.Office.Interop.Word.ApplicationClass applicationWord = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document modelloBusta = new Microsoft.Office.Interop.Word.Document(); try { modelloBusta = applicationWord.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 isVisible,ref missing, ref missing, ref missing, ref missing); modelloBusta.Activate(); } catch (COMException eccezione){ Console.Write(eccezione); modelloBusta.Application.Quit(ref missing, ref missing, ref missing); }
In the windows task manager the process is present, but the “word document” doesn’t appear (the application does not start).
What is the problem?
Thanks in advance.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need to make sure that the Word application window actually is made visible when automating Word like that:
var applicationWord = new Microsoft.Office.Interop.Word.Application(); applicationWord.Visible = true;
Method 2
first add the dll of office.interop by adding directly to the resources then add this using directive:
using Microsoft.Office.Interop.Word;
and use the following code
Application ap = new Application(); Document document = ap.Documents.Open(@"C:invoice.docx");;
Method 3
http://support.microsoft.com/kb/257757
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.
Document document = new Document(); document.LoadFromFile("test.doct");
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0