2012-04-18 42 views
2

我使用此代碼將數據發送到Word模板併發送到客戶端。錯誤「此命令不可用,因爲服務器上沒有文檔」

 protected void Button1_Click(object sender, EventArgs e) 
    { 
     object missing = System.Reflection.Missing.Value; 
     Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application(); 

     string DocFilePath = ""; 
     //string FilePath = System.Windows.Forms.Application.StartupPath; 
     object fileName = Server.MapPath(@"~\asset\wordtemplates\Estelam.dot"); 
     DocFilePath = fileName.ToString(); 

     FileInfo fi = new FileInfo(DocFilePath); 
     if (fi.Exists) 
     { 
      object readOnly = false; 
      object isVisible = true; 

      object LetterNo = "LetterNo"; 
      object LetterDate = "LetterDate"; 
      object Attachment = "Attachment"; 

      object To = "To"; 
      object From = "From"; 

      object Fname = "Fname"; 
      object Lname = "Lname"; 
      object FatherName = "FatherName"; 
      object IDNumber = "IDNumber"; 
      object BirthDate = "BirthDate"; 
      object BirthLocation = "BirthLocation"; 
      object PersonelID = "PersonelID"; 
      object MembershipDate = "MembershipDate"; 
      object Membership = "Membership"; 

      object Degree = "Degree"; 
      object Level = "Level"; 
      object Guild = "Guild"; 
      object Janbazi = "Janbazi"; 
      object Esarat = "Esarat"; 
      object Education = "Education"; 
      object Field = "Field"; 
      object Education2 = "Education2"; 
      object DocLocation = "DocLocation"; 

      Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, 
       ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
       ref isVisible, ref isVisible, ref missing, ref missing, ref missing); 

      //Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName); 

      WordApp.ActiveDocument.FormFields.get_Item(ref LetterNo).Result = TextBox_LetterNo.Text; 

在當地良好和正確的此代碼的工作,但是當我發佈這個網站,並投入到我的服務器我得到這個錯誤的運行時間。

This command is not available because no document is open. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.InteropServices.COMException: This command is not available because no document is open.

Source Error:An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

爲什麼它無法在服務器中打開?如何解決這個錯誤?

我使用Windows7,VS2010,SQLServer2008,office2010,並在服務器上使用Windows Server 2008,IIS7,office2010。

我應該爲服務器上的這個錯誤做些什麼?

+0

您不能從服務器應用程序(如ASP.NET Web應用程序)使用Office Automation。 Office產品是_desktop_產品,並且它們在服務環境中無法正常工作。 – 2012-04-18 06:14:09

回答

1

你缺少桌面文件夾改變,檢查here

您還需要配置用於自動執行Office應用程序的OfficeAutomationUser帳戶(如果尚未完成),請檢查this

注意:建議不要在您的服務器上安裝Office自動化。嘗試使用Open Xml sdk來自動化辦公應用程序。

0

閱讀這個word in asp那裏你會看到示例VB,但我想你會得到需要的信息。

0

您是否嘗試將模板路徑放在服務器的配置文件中? 服務器路徑不相對「D://項目文件夾/模板文件夾」。 然後讀取應用程序把它的配置文件的路徑, 將使它更容易在未來wihtout重新發布您的網站OfficeAutomation

相關問題