我有一個asp.net應用程序,它將上傳的ms doc轉換爲pdf,使用Microsoft.Office.Interop.Word.ApplicationClass.It在我的本地機器上正常工作,但它不能在部署的IIS服務器上工作。那裏有任何可執行文件需要權限才能獲得輸出。IIS可轉換權限將文檔轉換爲pdf?
錯誤是: 具有CLSID檢索COM類工廠部件{000209FF-0000-0000-C000-000000000046}失敗,原因是以下錯誤:80070005
我的代碼如下:
public class Word2Pdf
{
private Microsoft.Office.Interop.Word.ApplicationClass MSWordDoc;
object UnknownType = Type.Missing;
public string Word2PdfCOnversion(object InputLocation, object OutputLocation)
{
string result;
try
{
//if (MSWordDoc == null)
MSWordDoc = new Microsoft.Office.Interop.Word.ApplicationClass();
MSWordDoc.Visible = false;
MSWordDoc.Documents.Open(ref InputLocation, //Input File Name Location
ref UnknownType, // Conversion Conformation
ref UnknownType, // Set ReadOnly Property
ref UnknownType, // Add to the Recent Files
ref UnknownType, // Document Password Setting
ref UnknownType, // Password Templete
ref UnknownType, // Revert
ref UnknownType, // Write Password to Document
ref UnknownType, // Write Password Template
ref UnknownType, // File Format
ref UnknownType, // Encoding File
ref UnknownType, // Visibility
ref UnknownType, // To Open or Repair
ref UnknownType, // Document Direction
ref UnknownType, // Encoding Dialog
ref UnknownType); // XML Text Transform
MSWordDoc.Application.Visible = false; // To Invisible the Word Document
MSWordDoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize; // Minimize the Opened Word File.
object SavePDFFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
MSWordDoc.ActiveDocument.SaveAs(ref OutputLocation, //Output File Location
ref SavePDFFormat, // File Format
ref UnknownType, // Comment to PDF File
ref UnknownType, // Password
ref UnknownType, // Add to Recent File
ref UnknownType, // Write Password
ref UnknownType, // ReadOnly Propert
ref UnknownType, // Original Font Embeding
ref UnknownType, // Save Picture
ref UnknownType, // Saving Form Datas
ref UnknownType, // Save as AOVE Letter
ref UnknownType, // Encoding
ref UnknownType, // Inserting Line Breakes
ref UnknownType, // Allow Substitution
ref UnknownType, // Line Ending
ref UnknownType); // Add BiDi Marks
result = "Success";
}
catch (Exception)
{
result = "Error";
}
finally
{
if (MSWordDoc == null) { }
else { MSWordDoc.Documents.Close(ref UnknownType, ref UnknownType, ref UnknownType); }
MSWordDoc.Quit(ref UnknownType, ref UnknownType, ref UnknownType);
}
return result;
}
}
您正在使用的Com Compenent是否在服務器上註冊? – Deepesh
如果您使用的是最新版本的.Net,則不需要所有'參考UnknownType'參數。 .Net 3.5和更高版本允許這些參數是可選的,如果省略默認爲'Type.Missing'。可以使代碼更短,更容易掃描... YMMV – Grhm