服务器上出现错误“此命令不可用,因为没有打开任何文档”

本文关键字:因为 文档 任何 错误 命令 服务器 | 更新日期: 2023-09-27 18:31:40

我使用此代码将数据发送到 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;

代码在本地良好且正确的情况下工作,但是当我发布此站点并放入我的服务器时,我会收到此错误运行时。

此命令不可用,因为没有打开任何文档。 说明:执行 当前 Web 请求。请查看堆栈跟踪以获取更多信息 有关错误及其在代码中起源位置的信息。 异常详细信息:System.Runtime.InteropServices.COMException:this 命令不可用,因为没有打开任何文档。

源错误:执行过程中生成了未处理的异常 的当前网络请求。有关原产地和的信息 可以使用异常堆栈识别异常的位置 跟踪如下。

为什么它无法在服务器中打开?如何解决此错误?

我使用Windows7,VS2010,SQLServer2008,office2010,在服务器中使用Windows Server 2008,IIS7,office2010。

对于服务器上的此错误,我应该怎么做?

服务器上出现错误“此命令不可用,因为没有打开任何文档”

您缺少办公自动化的桌面文件夹,请查看此处

您还需要配置OfficeAutomation用户帐户(如果尚未配置)以自动化Office应用程序,请检查此内容

注意:不建议在服务器上安装 Office 自动化。尝试使用 Open Xml sdk 来自动化办公应用程序。

在asp中阅读这个词,您将看到示例VB,但我认为您将获得所需的信息。

您是否尝试将服务器上的模板路径放在配置文件中?服务器路径不是相对的"D://Project 文件夹/模板文件夹"。然后读取应用程序中的路径,将其放在配置文件中将使将来更容易更改,而无需重新发布您的网站