Iterate through pages in Word and find pages contains image

人走茶凉 提交于 2020-01-05 04:16:10

问题


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).

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

回答1:


A Document has a Shapes Collection 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:

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:

shp.Anchor.Information(wdActiveEndPageNumber)

There is a Pages Collection but it is not as useful 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 Rectangles collection and use RectangleType to try to determine if the current Rectangle is an image.



来源:https://stackoverflow.com/questions/18024209/iterate-through-pages-in-word-and-find-pages-contains-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!