【问题标题】:Word VBA Shapes Using Item使用项目的 Word VBA 形状
【发布时间】:2016-12-26 18:32:14
【问题描述】:

我有以下代码:

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) 将给出“0”的sr.count,但如果我更改为.item(2).item(3),文本框将在特定页面上找到。任何帮助将不胜感激。

【问题讨论】:

    标签: vba ms-word shapes


    【解决方案1】:

    在此处阅读第 2 项:

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

    我们最近对 Word 做了很多工作,它的行为正如文章中所说的那样,Word 和 Pages 不能很好地结合在一起。

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

    我也会质疑您尝试做的事情的有效性。如果一个人的分辨率比另一个人高得多,那么可能在一个人的机器上位于页面顶部和底部的形状可能在另一台机器上不存在。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2022-01-15
        • 1970-01-01
        • 2021-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多