2013-03-19 65 views
0

有沒有方法可以訪問形狀對象的起點和終點座標?我正在使用Excel 2010中的遺留文件(我認爲從Excel 2003開始)。msoLine類型的形狀對象 - 訪問點座標的方法?

給定一個msoFreeform對象,我可以使用類似訪問不同的座標依次爲:

With myDocument.Shapes(i) 
    If .Type = msoFreeform Then 
     nodeCount = .Nodes.Count 
     For k = 1 To nodeCount 
     pointsArray = .Nodes.Item(k).Points 
     X1 = pointsArray(1, 1) 
     Y1 = pointsArray(1, 2) 
     Next k 
    End If 
    End With 

但這種方法不能用於msoLine對象與.Nodes.Item(k).Points返回什麼,即使.Nodes.Count回報2的起點和終點。

我錯過了什麼嗎?

回答

1

這工作:

'The "flips" helps to work out which pair of corners of an imaginary rectangle surrounding the line represents the correct diagonal. 

子testLineCoords()

Dim bHflip As Boolean 
Dim bVflip As Boolean 
Dim nBegin As Long 
Dim nEnd As Long 
Dim oShape As Shape 
Dim aC(1 To 4, 1 To 2) As Double 

Set oShape = ShTmp.Shapes("MyLine") 
With oShape 
    aC(1, 1) = .Left:   aC(1, 2) = .Top 
    aC(2, 1) = .Left + .Width: aC(2, 2) = .Top 
    aC(3, 1) = .Left:   aC(3, 2) = .Top + .Height 
    aC(4, 1) = .Left + .Width: aC(4, 2) = .Top + .Height 

    bHflip = .HorizontalFlip 
    bVflip = .VerticalFlip 
End With 

If bHflip = bVflip Then 
    If bVflip = False Then 
     ' down to right 
     nBegin = 1: nEnd = 4 
    Else 
     ' up to left 
     nBegin = 4: nEnd = 1 
    End If 
ElseIf bHflip = False Then 
    ' up to right 
    nBegin = 3: nEnd = 2 
Else 
    ' down to left 
    nBegin = 2: nEnd = 3 
End If 

Debug.Print "---------------------------------" 
Debug.Print "Begin X:Y" 
Debug.Print aC(nBegin, 1); aC(nBegin, 2) 
Debug.Print "End X:Y" 
Debug.Print aC(nEnd, 1); aC(nEnd, 2) 

末次

可惜我不能把它的信用: Original solution

Regards, Emiel

+0

啊。當然。如果我已經檢查過對象屬性,可能可以解決這個問題......任何情況下,謝謝你的答案。 – Will 2013-05-14 14:21:45

0

如果你想獲得X/Y的起點和終點爲msoLine方法如下:

Dim myMsoLine As Shape 
Set myMsoLine = ActiveSheet.Shapes(3) 
Dim X1, X2, Y1, Y2 
'points of msoLine 

With myMsoLine 
    X1 = .Left 
    Y1 = .Top 
    X2 = .Width + .Left 
    Y2 = .Height + .Top 
End With 

Debug.Print X1, Y1, X2, Y2 

一般來說,有在msoLine形狀在這種情況下沒有節點。 (測試爲Excel 2010)

+0

感謝您的回答,並確認節點集合似乎無法訪問'msoLine'對象。 我已經考慮過這種方法,但實際上得到的是該行的邊界框,如果該行從左上角運行到右下角,則座標獲得鏡像關於該行中心點的鏡像,以便試圖重新創建行會導致「翻轉」副本。 – Will 2013-03-19 12:29:56