Office Interop nie działa w systemie Windows

Mam bardzo dziwny problem z Microsoft Office.

Mam wspólną bibliotekę, której jedynym celem jest otwieranie dowolnego typu pliku dokumentu ze słowami (przez pełną ścieżkę pliku ...) i zapisywanie tego otwartego dokumentu słownego jako pliku pdf.

Dziwny problem polega na tym, że jeśli zużyję tę bibliotekę z usługi systemu Windows, za każdym razem, gdy próbuje otworzyć dokument programu Word, otrzymuję wartość null ... aka, dokument programu Word nigdy nie został otwarty.

Jeśli jednak korzystam z biblioteki z aplikacji WPF lub Windows Form, nigdy nie mam żadnych problemów. Zdaję sobie sprawę, że istnieją problemy z wątkami (Single Thread Appartment), jednak nie mam pojęcia, jak to naprawić, aby działało z usługi Windows. :( :( :(

Byłbym wdzięczny za każdą pomoc! Błąd, który otrzymuję, jest następujący:

Wyjątek Komunikat: {"Odwołanie do obiektu nie jest ustawione na wystąpienie obiektu."} (Odwołanie do dokumentu słowa). Wyjątek wewnętrzny: Null; HResult: -2147467261. Dane: ListDictionaryInternal z 0 wpisami; Śledzenie stosu: w DocumentConverter.ToPdf (String currentWorkingFolderPath, String pathToDocumentToConvert) w c: Pliki projektu ... DocumentConverter.cs: line 209

Oto funkcja biblioteki. Wymaga odniesienia Microsoft Office, które jest tworzone przez Visual Studio Tools for Office.

private string ToPDF(string currentWorkingFolderPath, string pathToDocumentToConvert)
{
    string temporaryPdfFolderPath = Path.GetFullPath(currentWorkingFolderPath + "\\pdf\\");
    string temporaryPdfFilePath = Path.GetFullPath(temporaryPdfFolderPath + "\\pdffile.pdf");

    if (!FileSystem.CreateDirectory(temporaryPdfFolderPath))
    {
        return null;
    }

    try
    {
        Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();

        object objectMissing = System.Reflection.Missing.Value;

        wordApplication.Visible = false;
        wordApplication.ScreenUpdating = false;

        FileInfo wordFile = new FileInfo(pathToDocumentToConvert);

        Object fileName = (Object)wordFile.FullName;

        // This is where it breaks when called from windows service. Use the dummy value as a placeholder for optional arguments
        Document wordDocument = wordApplication.Documents.Open(ref fileName, ref objectMissing,            
            true, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,            
            ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,            
            ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing);



        object outputFileName = (object)temporaryPdfFilePath;
        object fileFormat = WdSaveFormat.wdFormatPDF ;

        // Save document into PDF Format
        wordDocument.SaveAs(ref outputFileName,
            ref fileFormat, ref objectMissing, ref objectMissing,
            ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
            ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
            ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing);

        // Close the Word document, but leave the Word application open.
        // doc has to be cast to type _Document so that it will find the
        // correct Close method.                
        object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
        ((_Document)wordDocument).Close(ref saveChanges, ref objectMissing, ref objectMissing);

        wordDocument = null;

        // word has to be cast to type _Application so that it will find
        // the correct Quit method.
        ((Microsoft.Office.Interop.Word._Application)wordApplication).Quit(ref objectMissing, ref objectMissing, ref objectMissing);

        wordApplication = null;

    }
    catch (Exception ex)
    {
        //logging code
        return null;
    }

    return temporaryPdfFilePath;
}

questionAnswers(5)

yourAnswerToTheQuestion