0

是否有访问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起点和终点也是如此。

我错过了什么吗?

4

2 回答 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)

结束子

不幸的是,我不能相信它: 原始解决方案

问候, 埃米尔

于 2013-05-13T11:11:36.930 回答
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 测试)

于 2013-03-19T11:16:26.447 回答