2016-08-19 99 views
1

我有下面的代码:字形状VBA项目使用

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),将在特定页面上找到文本框。任何帮助,将不胜感激。

回答

0

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

0

这里有第2项的读:

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

我们已经做了很多工作,与Word和最近它表现为文章称在Word和页面不顺利起来。 (1)应提供该页面上的所有矩形,但我怀疑它对页面感到困惑(要记住,每次删除一个形状时,整个文档将移动并形成一页纸上的形状现在将在另一个)。

我也会质疑你正在尝试做什么的有效性。如果一个人的分辨率高于另一个人的分辨率,那么一个人机器上页面顶部和底部的形状可能不会出现在另一个人机器上。