提示:页码pageNum是从1开始,其中最后一页的时候,我出现了只能删除内容情况,需要将开始index-1,亲测是对的,没有深究原理,有了解的前辈可以指导一下。
//删除指定的第几页
public void DeletePageFromWord(object pageNum)
{//下面是取得打开文件的页数Object missing = System.Reflection.Missing.Value;object objWhat = Word.WdGoToItem.wdGoToPage;object objWhich = Word.WdGoToDirection.wdGoToAbsolute;Word.Range range1 = _wordDocument.GoTo(ref objWhat, ref objWhich, ref pageNum, ref missing);Word.Range range2 = range1.GoToNext(Word.WdGoToItem.wdGoToPage);object objStart = range1.Start;object objEnd = range2.Start;if (objStart.Equals(objEnd)){objStart = (int)objStart - 1;objEnd = _wordDocument.Characters.Count;}object Unit = (int)Word.WdUnits.wdCharacter;object Count = 1;_wordDocument.Range(ref objStart, ref objEnd).Delete(ref Unit, ref Count);
}
C#之Word光标移动GoTo方法,参考下面这个博客,很详细
https://www.cnblogs.com/xinaixia/p/4896809.html