msoLine类型的形状对象 - 访问点坐标的方法?

时间:2013-03-19 10:47:30

标签: excel-vba excel-2010 vba excel

是否有方法可以访问msoLine形状对象的起点和终点的坐标?我正在使用Excel 2010中的旧文件(我认为是Excel 2003)。

给定一个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,也不会返回任何内容。

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

这有效:

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

Sub 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)

End Sub

不幸的是我不能相信它: Original solution

此致 Emiel

答案 1 :(得分:0)

如果您想获得msoLine的X / Y起点和终点,请执行以下操作:

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测试)