using Microsoft.Office.Interop.Word;
//启动Word程序
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object oMissing = System.Reflection.Missing.Value;
object filePath = ""; //这里是Word文件的路径
//打开文件
Document wordDoc = wordApp.Documents.Open(
ref filePath, 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);
//下面是取得打开文件的页数
int pages = wordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref oMissing);
object objWhat = Word.WdGoToItem.wdGoToPage
object objWhich = Word.WdGoToDirection.wdGoToAbsolute;
object objPage = 2;//指定页
Word.Range range1 = wordDoc.GoTo(ref objWhat, ref objWhich, ref objPage, ref oMissing);
Word.Range range2 = range1.GoToNext(Word.WdGoToItem.wdGoToPage);
object objStart = range1.Start;
object objEnd = range2.Start;
if (range1.Start == range2.Start)
objEnd = wordDoc.Characters.Count;//最后一页
string str=wordDoc.Range(ref objStart, ref objEnd).Text;
MessageBox.Show(str);
if(string.IsNullOrEmpty(str.Trim()))
{
object Unit = (int)Word.WdUnits.wdCharacter;
object Count = 1;
wordDoc.Range(ref bjStart, ref objEnd).Delete(ref Unit, ref Count);
}
wordDoc.Save();
//关闭文件
wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
//退出Word程序
wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
C# 删除word的指定页
最新推荐文章于 2024-11-04 13:18:03 发布