我使用Microsoft.Office.Interop.Word
将word转换为pdf,这在我的本地计算机上运行良好。
但是当我将exe文件移动到服务器(服务器安装了microsoft office)时,它会向我显示异常。
Unhandled Exception: System.Runtime.InteropServices.COMException: Command failed
at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object& FileName, Objec
t& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles,
Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts,
Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELette
r, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Objec
t& LineEnding, Object& AddBiDiMarks)
at PDF_Converter.Program.ConvertWordToPdf(String sInputFile, String sOutputFi
le) in D:\Work\HtmlToPDF_Converter\HTML_PDF_Converter\IMAGE_PDF_Converter\Progra
m.cs:line 89
at PDF_Converter.Program.Main(String[] args) in D:\Work\HtmlToPDF_Converter\H
TML_PDF_Converter\IMAGE_PDF_Converter\Program.cs:line 30
以下是我的转换代码。
private static void ConvertWordToPdf(string sInputFile, string sOutputFile)
{
// Create a new Microsoft Word application object
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;
word.Visible = false;
word.ScreenUpdating = false;
if (File.Exists(sInputFile))
{
FileInfo wordFile = new FileInfo(sInputFile);
// 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 doc = 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);
doc.Activate();
object outputFileName = sOutputFile;
object fileFormat = WdSaveFormat.wdFormatPDF;
// Save document into PDF Format
doc.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)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = 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;
}
我有什么遗失的吗?
答案 0 :(得分:1)
我收到了与你提到的相同的错误。
检查服务器上安装的办公室是否在“另存为”选项中具有pdf类型?
如果不可用,请从下方链接安装 2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS
。
http://www.microsoft.com/en-in/download/details.aspx?id=7
我希望它能解决错误。