问题
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