vba 遍历 Word 中的页面并查找包含图像的页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18024209/
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-11 22:32:41  来源:igfitidea点击:

Iterate through pages in Word and find pages contains image

vbams-wordword-vba

提问by Huy Hoàng Ph?m

I want to iterate though every page in a word document, check if that page contains an images or not, and do something about that page (Set page margin and insert a break).

我想遍历 word 文档中的每一页,检查该页面是否包含图像,并对该页面执行一些操作(设置页边距并插入一个中断)。

For Each Page in Document.Pages
   If Page.ContainsImage Then
      Page.TopMargin = 0
      DoOtherStuff
   End If
Next

回答by Andy G

A Documenthas a ShapesCollection representing all the Shapes. Each Shape has an Anchor, using which we can get to the TopMargin, and other properties, of the shape's page:

ADocument有一个Shapes代表所有形状的集合。每个 Shape 都有一个Anchor,我们可以使用它来TopMargin访问形状页面的 和其他属性:

Sub JiggleAllShapes()
    Dim shp As Shape

    For Each shp In ActiveDocument.Shapes
        shp.Anchor.Paragraphs(1).Range.PageSetup.TopMargin = 0
    Next shp
End Sub

We can get the page number from the Anchor:

我们可以从以下位置获取页码Anchor

shp.Anchor.Information(wdActiveEndPageNumber)

There is a PagesCollection but it is not as useful IMO:

有一个PagesCollection 但它在 IMO 中没有那么有用:

Sub WhatAboutPages()
    Dim pge As Page

    For Each pge In ActiveDocument.ActiveWindow.Panes(1).Pages
        'Debug.Print pge.NothingUsefulHere
    Next pge
End Sub

With this approach you would have to delve into the Rectanglescollection and use RectangleTypeto try to determine if the current Rectangleis an image.

使用这种方法,您必须深入研究Rectangles集合并使用它RectangleType来尝试确定当前Rectangle是否为图像。