How to extract shape coordinates in Word VBA

Jon Fournier picture Jon Fournier · Jan 5, 2010 · Viewed 7.1k times · Source

I'm trying to write a VBA macro in Word that will extract shapes and build them in Visio. I'm having some trouble getting the X Y coordinates of the shape in the document. I have tried using the Top and Left properties of the shape objects. The Left property seems to work fine, but the Top doesn't seem to work properly. A shape near the top of the page can have the same top as a shape at the bottom, so the top doesn't seem to apply to the Y coordinate, which doesn't make sense to me.

Any thoughts or suggestions?

Answer

Todd Main picture Todd Main · Jan 5, 2010

Jon, the "Top" property should update as the shape changes location. Are you running a script similar to this:

Sub getShapeXY()

    Dim shp As Shape
    Set shp = ThisDocument.Shapes(1)

    shpOffsetX = shp.Left
    shpWidth = shp.Width
    x = shpOffsetX + shpWidth

    shpOffsetY = shp.Top
    shpHeight = shp.Height
    y = shpOffsetY + shpHeight

    Debug.Print shpOffsetX & ": OffsetX, " & shpWidth & ": Width, " & x & ": X"
    Debug.Print shpOffsetY & ": OffsetY, " & shpHeight & ": Height, " & y & ": Y"

End Sub