vba 在 Word 中删除页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22032569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:08:08  来源:igfitidea点击:

Delete page in Word

vbams-wordms-officeword-vba

提问by compyutech

I need VBA code to delete all sections(pages) except first in Word document

我需要 VBA 代码来删除 Word 文档中除第一个之外的所有部分(页面)

For this I use below code.

为此,我使用以下代码。

For Each oSec In ActiveDocument.Sections
    If oSec.Index <> 1 Then
        oSec.Range.Delete
    End If   
Next oSec

This works but does not delete second section only removes its content. If I removes if condition in code it removes content of first page.

这有效,但不会删除第二部分,只会删除其内容。如果我删除代码中的 if 条件,它会删除第一页的内容。

I want to preserve content of first page.

我想保留第一页的内容。

Please tell me where I am making mistake.

请告诉我我哪里出错了。

回答by Kazimierz Jawor

When deleting you need to include section break marks. Try to change this line:

删除时需要包含分节符。尝试更改此行:

oSec.Range.Delete

into this one:

进入这个:

ActiveDocument.Range(oSec.Range.Start - 1, oSec.Range.End).Delete

BTW,you should not think that page=section, they are different type of document units.

顺便说一句,您不应该认为 page=section,它们是不同类型的文档单元。