使用C#从word文档中删除特定页面 [英] Delete a particular page from a word document using C#

查看:360
本文介绍了使用C#从word文档中删除特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从word文档中删除特定页面。

假设我有一个word文档。我想用C#删除该文档的第3页。

我该怎么做?

我试过这个..但我只能删除页面的某些行。但不是整个文档的页面。例如,在我的代码中,我只删除了5行..但是我要删除整个页面。在textbox1中,我取的是我要删除的页面的值。



I want to delete a particular page from a word document.
Suppose I have a word document. i want to delete the 3rd page of that document using C#.
How shall I do it?
I tried this.. but i can only delete some lines of a page. But not the whole page of a document. As for example, here in my code I have deleted only 5 lines..but I was to delete the whole page. In textbox1, I am taking the value of the page no which I want to delete.

object missing = System.Reflection.Missing.Value;
object fileName = filePath;
object readOnly = false;
object isVisible = true;

//Start Word and open a document.
Word._Application oWord;
Word._Document oDoc1, oDoc2;
oWord = new Word.Application();
oWord.Visible = true;

oDoc1 = oWord.Documents.Open(ref fileName, ref missing, ref readOnly,
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref isVisible, ref missing,
    ref missing, ref missing, ref missing);

//Goto some specific page and insert a blank page or page break
int y = 0;
int.TryParse(textBox1.Text.ToString(), out y);
object gotoPage = Word.WdGoToItem.wdGoToPage;
object gotoNext = Word.WdGoToDirection.wdGoToNext;
object gotoCount = null;
object gotoName = y;

object unit = Word.WdUnits.wdLine;

//var lineCount =File.ReadLines(filePath).Count();
//The lines you want to delete...
object count = 5;
object extend = Word.WdMovementType.wdExtend;

oWord.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName);
oWord.Selection.MoveDown(ref unit, ref count, ref extend);
oWord.Selection.TypeBackspace();

推荐答案

你不能!没有以下功能:

You can't! There is no functionality like:
ActiveDocument.Pages(NoOfPage).Delete
'or
ActiveDocument.Pages(NoOfPage).Remove



Page用于可视化文档布局。您需要从该页面中选择所有段落,然后您才能删除页面。



更多:

页面对象(Word) [ ^ ]

解决了:通过VBA在Word 2003中删除页面? [ ^ ]

选择或引用Word对象模型中的页面 [ ^ ]





我写了宏w选择引用页面上的所有段落并删除它们。


Page is used to visualize document layout. You need to select all paragraphs from that page, then you'll be able to delete "page".

More:
Page Object (Word)[^]
Solved: Delete a Page in Word 2003 via VBA?[^]
Selecting or referring to a page in the Word object model[^]


I wrote macro which selects all paragraphs on referenced page and delete them.

Sub Test()

DeletePage ActiveDocument, 2

End Sub


Sub DeletePage(ByVal doc As Document, ByVal iPage As Integer)
Dim rngStart As Range, rngEnd As Range, i As Integer

Set rngStart = doc.GoTo(What:=wdGoToPage, Which:=iPage)
rngStart.Select
Set rngEnd = rngStart
Do While i < iPage + 1
    Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    Set rngEnd = Selection.Range
    i = rngEnd.Information(wdActiveEndPageNumber)
    If i > iPage Then
        Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
        Set rngEnd = Selection.Range
        Exit Do
    End If
Loop

doc.Range(Start:=rngStart.Start, End:=rngEnd.End).Select

If MsgBox("Are you sure you want to delete " & iPage & " page?", vbQuestion + vbYesNo + vbDefaultButton2) = vbYes Then
    Selection.Delete
End If

End Sub





注意:这是无效的方式。



[/ EDIT]



Note: It's ineffective way.

[/EDIT]


这篇关于使用C#从word文档中删除特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆