2

I'm successfully using Office automation on Windows Server 2008 R2 with Office 2007 in order to convert Office documents to PDFs. The code is rather simple:

public class WordConvert
{
    /// <summary>
    /// Converts a word file to PDF
    /// </summary>
    /// <param name="sourceFilePath">The path of the word file to convert</param>
    /// <param name="targetFilePath">The path of the PDF output file</param>
    public static void ConvertWord(string sourceFilePath, string targetFilePath)
    {
        object objTragetFileName = targetFilePath;
        Word.Application wordDocument = new Word.Application();
        try
        {
            OpenWord(sourceFilePath, wordDocument);
            SaveAsPDF(ref objTragetFileName, wordDocument);
        }
        finally
        {
            CloseWord(wordDocument);
        }
    }

    private static void OpenWord(object sourceFileName, Word.Application wordDocument)
    {
        wordDocument.Documents.Open(ref sourceFileName);
    }

    private static void SaveAsPDF(ref object targetFileName, Word.Application wordDocument)
    {
        object format = Word.WdSaveFormat.wdFormatPDF;
        wordDocument.ActiveDocument.SaveAs(ref targetFileName, ref format);
    }

    private static void CloseWord(Word.Application wordDocument)
    {
        if (wordDocument != null)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // 2nd time to be safe
            GC.Collect();
            GC.WaitForPendingFinalizers();

            Word.Documents documents = wordDocument.Documents;
            documents.Close();
            Marshal.ReleaseComObject(documents);
            documents = null;


            Word.Application application = wordDocument.Application;
            application.Quit();
            Marshal.ReleaseComObject(application);
            application = null;
        }
    }
}

The problem is that this code doesn't work on Windows Server 2012. The error received is:

System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

Running the code as interactive user (console app) works fine but it fails when running from IIS web app or from windows service (even with 'alow service to interact with desktop'). The user running the app has enough permissions (administrator) and the code works fine with Office 2010.

Any ideas?

Kara
  • 6,115
  • 16
  • 50
  • 57
basdanny
  • 79
  • 1
  • 6
  • Possible duplicate of [Server-side word automation](https://stackoverflow.com/questions/9810718/server-side-word-automation) – Lex Li Apr 20 '18 at 22:16

3 Answers3

4

I'll probably get downvoted for this answer, but I work in an enterprise environment where we have a product that uses Office Automation, and it is very problematic.

I've done research in this arena, and Microsoft itself recommends against doing Office Automation.

The following is directly from Microsoft's MSDN knowledge base

Microsoft does not recommend or support server-side Automation of Office.

Also

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: http://support.microsoft.com/kb/257757

Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
  • Obviously I'm aware of this. But it does not change the requirement to use Office. The reason is that Office convert its documents to pdf most accurately. – basdanny Jan 21 '13 at 10:14
0

The only solution found is to make the process that invokes Office API to run as interactive. It can be done by just running a console app (not the brightest idea for server solutions) or by creating some background service (e.g., windows service) and set it's station (SetProcessWindowStation).

basdanny
  • 79
  • 1
  • 6
0

One of the reason to see that error message is if server is missing SysWow64 folder. Here a link that might help you understand better.

http://per.lausten.dk/blog/2011/04/excel-automation-on-windows-server-2008-x64.html

messed-up
  • 493
  • 4
  • 12
  • 1
    Even if your answer might help OP, could you post the relevant information from that site? – Zippy Apr 22 '16 at 13:06