Office Interop Does Not Work in Windows Service

烂漫一生 提交于 2019-11-30 07:39:08
bleepzter

Per @Sameer S in his post: Is Office 2003 interop supported on Windows server 2008 ..?

Officially Microsoft Office 2003 Interop is not supported on Windows server 2008 by Microsoft.

But after a lot of permutations & combinations with the code and search, we came across one solution which works for our scenario.

The solution is to plug the difference between the way Windows 2003 and 2008 maintains its folder structure, because Office Interop depends on the desktop folder for file open/save intermediately. The 2003 system houses the desktop folder under systemprofile which is absent in 2008.

So when we create this folder on 2008 under the respective hierarchy as indicated below; the office Interop is able to save the file as required. This Desktop folder is required to be created under

C:\Windows\System32\config\systemprofile

AND

C:\Windows\SysWOW64\config\systemprofile

Thanks Guys..!

Office Interop is not supported by MS in server-like scenarios (like ASP.NET or Windows Service or similar) - see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 !

You will need to use some library to achieve what you want:

Once these folders have been created:

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

Make sure the schedule task is running with a profile having access to these folders.

Is your windows service have the 'run in interactive mode' option enabled? It may be failing because Word is attempting to display a UI, which for reasons which seem obvious, it cannot do.

Another possibility (and a pretty common problem people have) is that your Windows Service is running under an account that doesn't have permissions to access the file/folder you're attempting to pass to it. A WPF/Winforms program by contrast runs under the user's credentials.

If that is not the problem, are there any errors being thrown? Check the Windows Event Log and/or add some logging to see if there are some errors being silently thrown.

EDIT: I glanced through the MSDN documention (for Word Interop) and it doesn't appear that using VSTO is required. Can you attempt to do this using pure word interop and see if it works? This post has an example

Steven Leong

My ASP.net 2.0 office automation application work well in Windows 2008 Server 64 bits after follow the instruction from the link below. Hope it could help everyone.

http://emalvass.blogspot.com/2010/01/how-to-use-office-2007-automation-in.html

My sample program as below:

Imports Microsoft.Office.Interop.Word


Dim Result As Boolean = False

    Dim appWord As New Microsoft.Office.Interop.Word.Application
    Dim wordDocument As Microsoft.Office.Interop.Word.Document

    Try
        Dim oFile As New FileInfo(sPDFFile), sFilePath As String, sFileName As String, sFileNameArr() As String
        Dim sActualFileName As String

        sFileName = oFile.Name
        sFileNameArr = sFileName.Split(".")
        sActualFileName = sFileNameArr(0)
        sFilePath = oFile.DirectoryName & "\" & sActualFileName & ".pdf"

        wordDocument = appWord.Documents.Open(sPDFFile)

        wordDocument.ExportAsFixedFormat(sFilePath, WdExportFormat.wdExportFormatPDF)
        'wordDocument.ExportAsFixedFormat(sFilePath, 17)

        Result = True
        oFile = Nothing

    Catch ex As COMException
        sErrMsg = ex.InnerException.ToString
        Result = False
    Finally
        wordDocument.Close()
        wordDocument = Nothing
        appWord.Application.Quit()
        appWord = Nothing



    End Try


    Return Result
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!