using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; 
namespace word{ /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox SaveAs;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label wordText;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label result;
protected System.Web.UI.WebControls.Button Button1;


#region Web Form Designer generated code override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Page_Load(object sender, System.EventArgs e) { }
private void Button1_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.Value;
//取得Word文件保存路径
object filename=SaveAs.Text;
//创建一个名为WordApp的组件对象
Word.Application WordApp=new Word.ApplicationClass();
//创建一个名为WordDoc的文档对象
Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
//增加一表格
Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,1,1,ref Nothing,ref Nothing);
//在表格第一单元格中添加自定义的文字内容
table.Cell(1,1).Range.Text=wordText.Text;
//在文档空白地方添加文字内容
WordDoc.Paragraphs.Last.Range.Text="Wellcome To Aspxcn.Com";
//将WordDoc文档对象的内容保存为DOC文档
WordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
//关闭WordDoc文档对象 
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//返回结果
result.Text="文档路径:<a href='"+SaveAs.Text+"'>"+SaveAs.Text+"</a>(点击链接查看)<br>生成结果:成功!";
} }}
“/qqqq”应用程序中的服务器错误。
--------------------------------------------------------------------------------无法打开宏储存。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: 无法打开宏储存。源错误: 
行 65:  Word.Application WordApp=new Word.ApplicationClass();
行 66:  //创建一个名为WordDoc的文档对象
行 67:  Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
行 68:  //增加一表格
行 69:  Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,1,1,ref Nothing,ref Nothing);
 源文件: c:\inetpub\wwwroot\qqqq\index.aspx.cs    行: 67 堆栈跟踪: 
[COMException (0x800a175d): 无法打开宏储存。]
   Word.Documents.Add(Object& Template, Object& NewTemplate, Object& DocumentType, Object& Visible) +0
   word.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\qqqq\index.aspx.cs:67
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() +1277 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573

解决方案 »

  1.   

    你这代码是从帮助里面抄过来的吧。ref nothing算什么啊?
    Word.Document WordDoc=WordApp.Documents.Add();这样应该就可以了,后面也一样。不过我没有做过实际测试,说错了请原谅。
      

  2.   

    做个记号,谢谢高手指导!
    下一步我可能也要做asp.net方面的东西
      

  3.   

    可能是权限的问题吧,你在web.config里添加如下代码试试:<identity impersonate="true" userName="你电脑的用户名" password="密码" />
      

  4.   

    http://codeproject.com/aspnet/wordApplication.asp