使用项目的Word VBA形状

时间:2016-08-19 14:36:03

标签: vba ms-word shapes

我有以下代码:

Sub removeTopAndBottomMostShapesFromActiveDocument()

Dim shape As shape
Dim topShape As shape
Dim bottomShape As shape

Dim pageNum
For pageNum = 1 To ActiveWindow.Panes(1).Pages.Count

    Dim highestPoint, lowestPoint
    highestPoint = 999999
    lowestPoint = -999999

    Set topShape = Nothing
    Set bottomShape = Nothing

    Dim sr As ShapeRange
    Set sr =  ActiveWindow.Panes(1).Pages(pageNum).Rectangles.Item(1).Range.ShapeRange
    sr.Select
    For Each shape In sr
        If shape.Top < highestPoint Then
            Set topShape = shape
            highestPoint = shape.Top
        End If
        If shape.Top + shape.Height > lowestPoint Then
            Set bottomShape = shape
            lowestPoint = shape.Top + shape.Height
        End If
    Next

    If Not topShape Is Nothing Then
        topShape.Delete
    End If
    If Not bottomShape Is Nothing Then
        bottomShape.Delete
    End If

使用Set sr = Activewindow ...我无法弄清楚.item(1)的作用。我在Word文档中的文本框都是一样的。在某些网页.item(1)上会sr.count为“0”,但如果我更改为.item(2).item(3),则会在特定网页上找到文本框。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

在这里阅读第2项:

http://shaunakelly.com/word/word-development/selecting-or-referring-to-a-page-in-the-word-object-model.html

我们最近在Word上做了很多工作,它的行为就像文章所说,Word和Pages不能很好地结合在一起。

那.Items(1)应该提供该页面上的所有矩形,但我怀疑它对页面感到困惑(每次删除一个形状时都要记住整个文档将移动,并且一页上的形状将会现在在另一个)。

我还会质疑你要做的事情的有效性。如果一个人的分辨率比另一个人的分辨率高得多,那么一台机器上页面顶部和底部的形状可能不会出现在另一台人机上。

答案 1 :(得分:0)

我想我找到了一个解决方法。由于某种原因,文本框被放置在不同的项目中,即使它们是由完全相同的过程创建的。如果有人能够解释这一点,我将不胜感激。因此解决方案是使用.Item(i)从1到3创建一个循环,它抓取页面上的所有框并且似乎没有问题。

相关问题