!!!!强烈建议高手见来看看:.net替换word模板问题:在xp,word2000下运行正常,在win2000server,word2000下就会出错!
string strWordTemplate = Server.MapPath("../test/办文单.doc"); //这里是你的模板文件
Word.Application WordApp = new Word.ApplicationClass(); // 定义一个Word.Application 对象
Word.Document doc = OpenDoc(strWordTemplate,ref WordApp,1); //定义一个Word.Document 对象
try
{
//下面是从数据库取数据,不好意思这个代码有点烂
DataTable TempTable=this.CreateTable("Select * from Workflow_BW where AppID="+Convert.ToInt32(AppID)+" and ContentID="+Convert.ToInt32(ContentID));
if(TempTable.Rows.Count>0)
{
string TempTime=TempTable.Rows[0]["SWTime"].ToString().Trim();
int Pos=TempTime.IndexOf(" ");
string All=TempTime.Substring(0,Pos);
int Pre=All.IndexOf("-");
int Next=All.LastIndexOf("-");
string Year=All.Substring(0,Pre).Trim();
string Month=All.Substring(Pre+1,Next-Pre-1).Trim();
string Day=All.Substring(Next+1,All.Length-Next-1).Trim();
foreach(Word.Bookmark BM in doc.Bookmarks) //这是最关键的地方:对文档的所有书签进行便利匹配
{
switch(BM.Name)
{
case "Advice": //替换Advice书签的内容,其他一样
BM.Select();
BM.Range.Text=this.CreateTable("Select Advice from Workflow_Advice where AppID="+Convert.ToInt32(AppID)+" and ContentID="+Convert.ToInt32(this.ContentID)+" and StepID=1").Rows[0]["Advice"].ToString().Trim();
break;
case "Day":
BM.Select();
BM.Range.Text=Day;
break;
case "LWDW":
BM.Select();
BM.Range.Text=TempTable.Rows[0]["LWDW"].ToString().Trim();
break;
case "LWH":
BM.Select();
BM.Range.Text=TempTable.Rows[0]["SWH"].ToString().Trim();
break;
case "Month":
BM.Select();
BM.Range.Text= Month;
break;
case "NowYear":
BM.Select();
BM.Range.Text=Year;
break;
case "Subject":
BM.Select();
BM.Range.Text=TempTable.Rows[0]["Subject"].ToString().Trim();
break;
case "SWH":
BM.Select();
BM.Range.Text=TempTable.Rows[0]["LSH"].ToString().Trim().Substring(4,TempTable.Rows[0]["LSH"].ToString().Trim().Length-4);
break;
}
}
}
object fn = (object)Server.MapPath("../test/temp.doc");
doc.SaveAs(ref fn, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing); //这里是另存为一个一个文件
Response.Redirect("../test/temp.doc"); //直接打开用ie打开另存的文件,然后可直接调用ie里的打印功能
//doc.SaveAs(ref fn, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing,ref missing,ref missing,ref missing,ref missing);
}
catch(Exception err)
{
this.tbXml.Text = err.ToString();
}
finally
{
doc.Close(ref missing,ref missing,ref missing);
WordApp.Quit(ref missing,ref missing,ref missing);
WordApp = null;
}
}
上面代码在xp office2000下正常运行,但在win2000server office2000抱下面错误
消息筛选器显示应用程序正在使用中。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Runtime.InteropServices.COMException: 消息筛选器显示应用程序正在使用中。
源错误:
行 132: //打开word模板
行 133: string strWordTemplate = Server.MapPath("../test/办文单.doc");
行 134: Word.Application WordApp = new Word.ApplicationClass();
行 135: Word.Document doc = OpenDoc(strWordTemplate,ref WordApp,1);
行 136: try
进程里面的winword.exe2000下杀不掉,真是郁闷