vba 如何在Word VBA中提取形状坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2007636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:10:51  来源:igfitidea点击:

How to extract shape coordinates in Word VBA

vbams-wordvisio

提问by Jon Fournier

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.

我正在尝试在 Word 中编写一个 VBA 宏,它将提取形状并在 Visio 中构建它们。我在获取文档中形状的 XY 坐标时遇到了一些麻烦。我曾尝试使用形状对象的 Top 和 Left 属性。Left 属性似乎工作正常,但 Top 似乎无法正常工作。页面顶部附近的形状可以与底部的形状具有相同的顶部,因此顶部似乎不适用于 Y 坐标,这对我来说没有意义。

Any thoughts or suggestions?

有什么想法或建议吗?

采纳答案by Todd Main

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

Jon,“顶部”属性应该随着形状改变位置而更新。您是否正在运行类似于此的脚本:

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