当前位置:编程学习 > C#/ASP.NET >>

求助,好心人进。在线等,急啊

我帮老板开发个网站,主要是文档校验。先输入一个模板,然后再输入文档。会把输入的文档的格式与模板不同的地方指出。我把网站挂到局域网上,局域网内的机器可以正确访问。挂到服务器上时,在服务器端的vs2008上可以正常运行,可是外网访问时却出错了。错误时,当上传模板时提示说Object reference not set to an instance of an object. 部分代码如下
  protected void Button1_Click(object sender, EventArgs e)//上传模板
  {
   try{
  if (FileUpload0.HasFile)
  {

  FileUpload0.SaveAs("C:\\Upload\\template.doc");
    
  oTemplateWord = new Word.Application();
  oTemplateWord.Visible = true;


  object templateFileName = "C:\\Upload\\template.doc";
  oTemplateDoc = oTemplateWord.Documents.Open(ref templateFileName,
  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);

  }
  else
  return;
    

  Word.Style oStyle = null;
  int paraCount = oTemplateDoc.Paragraphs.Count;
  for (int j = 1; j < paraCount; j++)
  {
  if (isBlankLine(oTemplateDoc, j))
  continue;
  if (bodyText[0].name != null && otherTitle[2].name != null)
  break;
  switch (oTemplateDoc.Paragraphs[j].OutlineLevel.ToString())
  {
  case "wdOutlineLevelBodyText":
  oStyle = (Word.Style)oTemplateDoc.Paragraphs[j].get_Style();
  if (oStyle.NameLocal.Equals("标题"))
  {
  ProcessTitle(oTemplateDoc, j);
  }
  else
  {
  if (otherTitle[0].name != null)
  ProcessBodyText(oTemplateDoc, j);
  else
  {
  string tmp = oTemplateDoc.Paragraphs[j].Range.Text;
  string tmp1 = tmp.Replace(" ", "");
  //MessageBox.Show(tmp1 + tmp1.Substring(0, 2));
  if (tmp1.Substring(0, 2).Equals("摘要"))
  j = ProcessChineseAbstract(oTemplateDoc, j);
  else if (tmp1.Substring(0, 2).Equals("目录"))
  j = ProcessCatalog(oTemplateDoc, j);
  else if (tmp1.Substring(0, 8).Equals("ABSTRACT"))
  j = ProcessEnglishAbstract(oTemplateDoc, j);
  }
  }
  break;

  default:
  ProcessOtherTitle(oTemplateDoc, j);
  break;
  }
  }


  oTemplateDoc.Close(ref oMissing, ref oMissing, ref oMissing);
  oTemplateWord.Quit(ref oMissing, ref oMissing, ref oMissing);
  Label1.Text = "模板上传成功";
}catch{
   。。。。。 }
}
我当时觉得是oTemplateDoc = oTemplateWord.Documents.Open(ref templateFileName,
  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);出问了。但不知是什么原因。恳求各位大侠帮忙。不胜感激。不加try语句。会说使用了没赋值的变量int paraCount = oTemplateDoc.Paragraphs.Count;此句报错。高手们这是什么错误啊。该不会是访问权限的原因吧
--------------------编程问答-------------------- 使用相对路径
Server.MapPath
--------------------编程问答-------------------- 刚才试了一下使用想多路径也不行啊 --------------------编程问答-------------------- Object reference not set to an instance of an object,这是告诉你为将对象引用设置到对象的实例,也就是说你用的对象没有初始化。其实是告诉你服务器上还没有安装 Office。 --------------------编程问答-------------------- 一句话:这是常见的空引用错误。 --------------------编程问答-------------------- 当然,路径问题也要改一下,用 Server.MapPath() 方法。 --------------------编程问答-------------------- 难道在声明的时候必须要设为null吗?我装office了啊 --------------------编程问答-------------------- 这种空引用错误该怎么该啊 --------------------编程问答-------------------- 具体是哪一行代码出了错? --------------------编程问答-------------------- 是 oTemplateWord = new Word.Application(); 出错吗? --------------------编程问答-------------------- 如果加了try语句直接就跳到catch了我觉得是oTemplateDoc = oTemplateWord.Documents.Open(ref templateFileName,
  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);
  }不能正确打开word。所以下面无法执行。如果不加try
就显示int paraCount = oTemplateDoc.Paragraphs.Count。错误
--------------------编程问答-------------------- 你的 oTemplateDoc 在哪里被实例化的? --------------------编程问答--------------------
引用 9 楼 guohelv0519 的回复:
是 oTemplateWord = new Word.Application(); 出错吗?

这个没法测试啊。我用try直接就跳到catch了。并且本地运行还没错。无法测试哪个地方错了啊 --------------------编程问答-------------------- 知道了,将你的文件路径修改一下吧。应该是路径错误造成的错误。用 Server.MapPath()。 --------------------编程问答--------------------
引用 11 楼 guohelv0519 的回复:
你的 oTemplateDoc 在哪里被实例化的?
oTemplateDoc = oTemplateWord.Documents.Open(ref templateFileName,
  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);这一句将oTemplateDoc 实例化。但是这句却不能执行 --------------------编程问答-------------------- oTemplateDoc = oTemplateWord.Documents.Open(),这是接受 open 方法的返回值,如果你的路径不对,返回值就是空的,所以后续逻辑出了空引用的问题。 --------------------编程问答-------------------- 我的路径应该是对的。就放在了C盘下。我也曾改在当前目录。但还是会出错。我奇怪的是为什么打不开啊。在局域网都能打开啊 --------------------编程问答-------------------- 在服务器上不要用绝对路径。一定要用服务器映射机制。 --------------------编程问答-------------------- 服务器映射机制。能说的清楚些吗?我刚学C#。谢谢 --------------------编程问答-------------------- 比如说,如果将模板文件存放在网站根目录下 upload 文件夹中,那么你该将上述 C:\xxxx 改为:Server.MapPath("~/upload/template.doc")。 --------------------编程问答--------------------
引用 19 楼 guohelv0519 的回复:
比如说,如果将模板文件存放在网站根目录下 upload 文件夹中,那么你该将上述 C:\xxxx 改为:Server.MapPath("~/upload/template.doc")。
好心人还在吗?刚才调试了一下,还是不行
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,