2016-08-19 101 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)應提供該頁面上的所有矩形,但我懷疑它對頁面感到困惑(要記住,每次刪除一個形狀時,整個文檔將移動並形成一頁紙上的形狀現在將在另一個)。

我也會質疑你正在嘗試做什麼的有效性。如果一個人的分辨率高於另一個人的分辨率,那麼一個人機器上頁面頂部和底部的形狀可能不會出現在另一個人機器上。