Използвах 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;
}
Има ли нещо, което ми липсва?