word分页问题
执行代码后,第1页与第2页之间插入分页符无效,之后的的分页符就没事,不知怎么回事,大家帮忙吧:
void test()
{
//开启word对象
object Nothing = System.Type.Missing;
object format = System.Type.Missing;
Word.Application wordApp = new Word.ApplicationClass();
string rootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
if(rootPath.EndsWith("\\"))
{
rootPath = rootPath.Substring(0, rootPath.Length - 1);
}
string wordFileName = DateTime.Now.ToString("yyyyMMddhhmmssffff") + ".doc";
object srcFileName = rootPath + "\\temp\\" + wordFileName;
Word.Document wordDoc2 = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc2.Paragraphs.Last.Range.InsertFile(@"D:\Projects\SZPJXXB\temp\201105131120285312\jbszdy.html", ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//光标移动到文档最后一行。
wordApp.Selection.WholeStory();
object MoveUnit = Word.WdUnits.wdStory;
object MoveExtend = Type.Missing;
wordApp.Selection.EndKey(ref MoveUnit, ref MoveExtend);
//插入分页符
object BreakType = Word.WdBreakType.wdPageBreak;
wordApp.Selection.InsertBreak(ref BreakType);
wordDoc2.Paragraphs.Last.Range.InsertFile(@"D:\Projects\SZPJXXB\temp\201105130236554062\zbxxdy.html", ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordApp.Selection.EndKey(ref MoveUnit, ref MoveExtend);
wordApp.Selection.InsertBreak(ref BreakType);
wordDoc2.Paragraphs.Last.Range.InsertFile(@"D:\Projects\SZPJXXB\temp\201105131120285312\jbszdy.html", ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordApp.Selection.EndKey(ref MoveUnit, ref MoveExtend);
wordApp.Selection.InsertBreak(ref BreakType);
wordDoc2.Paragraphs.Last.Range.InsertFile(@"D:\Projects\SZPJXXB\temp\201105130236554062\zbxxdy.html", ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc2.SaveAs(ref srcFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc2.Close(ref Nothing, ref Nothing, ref Nothing);
if (wordDoc2 != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc2);
wordDoc2 = null;
}
//关闭wordApp
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
if (wordApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
wordApp = null;
}
}