Office Interop funktioniert nicht im Windows-Dienst

Ich habe ein sehr seltsames Problem mit Microsoft Office.

Ich habe eine gemeinsame Bibliothek, deren einziger Zweck es ist, den Dateityp eines Word-Dokuments zu öffnen (über einen vollständigen Dateipfad ...) und das geöffnete Word-Dokument als PDF-Datei zu speichern.

Das seltsame Problem ist, dass, wenn ich diese Bibliothek von einem Windows-Dienst aus konsumiere, immer dann, wenn versucht wird, das Word-Dokument zu öffnen, ich eine Null erhalte, aka, das Word-Dokument wurde nie geöffnet.

Wenn ich jedoch die Bibliothek aus einer WPF- oder Windows Form-Anwendung verwende, treten keine Probleme auf. Ich bin mir bewusst, dass es Probleme mit dem Threading gibt (Single Thread Appartment), aber ich habe keine Ahnung, wie ich es beheben kann, um aus dem Windows-Dienst heraus zu arbeiten. :( :( :(

Ich würde mich über jede Hilfe freuen! Der Fehler, den ich erhalte, ist der folgende:

Ausnahmemeldung: {"Objektreferenz nicht auf eine Instanz eines Objekts festgelegt."} (Bezieht sich auf das Word-Dokument). Innere Ausnahme: Null; HResult: -2147467261. Daten: ListDictionaryInternal mit 0 Einträgen; Stapelverfolgung: bei DocumentConverter.ToPdf (String currentWorkingFolderPath, String pathToDocumentToConvert) in c: \ Project Files ... \ DocumentConverter.cs: Zeile 209

Hier ist also die Bibliotheksfunktion. Hierfür ist die Microsoft Office-Referenz erforderlich, die von den Visual Studio-Tools für Office erstellt wird.

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;
}

Antworten auf die Frage(5)

Ihre Antwort auf die Frage