Click here to Skip to main content
15,887,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
In my Window Service, I am opening MS Word document by the following line:

doc1 = WordApp.Documents.Open(UpFile, ReadOnly: true);
I am using:

using Word = Microsoft.Office.Interop.Word;
The same code works fine in Console Application but not working in Window Service. Any suggestions? I wan't my code to run at a specific time in a day so I am creating Window Service. Any other alternative?

(My code opens word documents and calculates line/word/character count/date last saved/last commented by) 


What I have tried:

I tried coding the same in PHP but I am unable to get date last saved and the name of the person who last commented.
Posted
Updated 24-May-16 20:54pm
Comments
ZurdoDev 24-May-16 15:03pm    
1. What is the error?
2. Office apps do not support automation, even though you can do a lot. But Microsoft's position has always been that these are user driven applications and may not work right when automated.
3. I think the OpenXML SDK may help you.
ZurdoDev 24-May-16 15:03pm    
4. If it runs in a console app then use task scheduler to schedule the console app.
mariummalik22 24-May-16 15:18pm    
Thanks. I will try that now

You should not use Word interop in the server setting. Please see these Microsoft articles:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

And it's also better not to use it on other situations. The reasonable opportunities include Microsoft Open XML SDK and NPOI (NPOI - Home[^]). Please see my past answers referenced in this one: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA
 
Share this answer
 
To run Interop libraries on windows service needs much access permission I guess, windows services should running under system privilege and then it should access your word documents.
To give access create a folder named Desktop in following folders
- for x64 system is necessary to create the following folder:
C:\Windows\SysWOW64\config\systemprofile\Desktop
- for x86 system:
C:\Windows\System32\config\systemprofile\Desktop
(it looks a funny solution but works)
see below snippet
Hot Malva tech blog: How to use Office 2007 Automation in Windows 2008 service[^]

OR

Other alternative is to create a console application and use a teak scheduler to run your console application on specific time.
I have used above technique on interop application in console app and it works great with windows scheduler
(Note: the user used to create a windows scheduler should have administrative rights)
Hope it helps
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900