Microsoft.Office.Interop.Word.Document.saveas get命令在服务器上失败

时间:2018-03-08 12:59:20

标签: c# asp.net ms-word interop

我尝试使用Microsoft.Office.Interop.Word将convert .doc转换为.pdf 根据以下代码在我的电脑上工作,但当我上传到服务器时,我得到了

  

"命令失败"

服务器上的doc1.SaveAs;在我的机器上它正在工作

//***********************************************************
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

        // C# doesn't have optional arguments so we'll need a dummy value
        object oMissing = System.Reflection.Missing.Value;

        // Get list of Word files in specified directory
        //DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
        FileInfo wordFile = new FileInfo(FromLocation);

        word.Visible = false;
        word.ScreenUpdating = false;

        // Cast as Object for word Open method
        Object filename = (Object)wordFile.FullName;

        // Use the dummy value as a placeholder for optional arguments
        Microsoft.Office.Interop.Word.Document doc1 = word.Documents.Open(ref filename, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        doc1.Activate();

        object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
        object fileFormat = WdSaveFormat.wdFormatPDF;

        // Save document into PDF Format
        doc1.SaveAs(ref outputFileName,
            ref fileFormat, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        // 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)doc1).Close(ref saveChanges, ref oMissing, ref oMissing);
        doc1 = null;

        // word has to be cast to type _Application so that it will find
        // the correct Quit method.
        ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
        word = null;

        return outputFileName.ToString();

0 个答案:

没有答案