Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract shape coordinates in Word VBA

Tags:

ms-word

vba

visio

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?

like image 450
Jon Fournier Avatar asked Oct 25 '22 22:10

Jon Fournier


1 Answers

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
like image 113
Todd Main Avatar answered Nov 15 '22 05:11

Todd Main