这个问题不难,但是我试了2天都没找到解决方法:-(

jam021 2004-07-25 07:27:28
是关于word的.在word里画了2个矩型,我写了个2个宏,宏1的代码如下:
MsgBox "Left:" & Selection.ShapeRange.Left & " Top:" & Selection.ShapeRange.Top
宏2的代码如下:
Selection.ShapeRange.Top = 36
Selection.ShapeRange.Left = 0
这个36和0是其中一个矩型shape的top和left,这个宏2的意思就是把第2个矩型移动到和第一个矩型相同的top和left,在没有回车符的情况下是正常的,但是如果这个word里有很多的回车符后,那么就出问题了,大家可以试一下,执行宏2后那个矩型没有移动到和另一个矩型相同的top(left是正常的),后来我研究了一下好象是回车的问题,即根据回车的不同好象有好几个坐标的域,导致了top都是36,但却不在一个高度,请高手们帮忙了,谢谢!
...全文
172 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jam021 2004-07-26
  • 打赏
  • 举报
回复
多谢tech兄,给你加分了!
techgopher 2004-07-26
  • 打赏
  • 举报
回复
问题不是回车,而是anchor(锚?)的位置。你看看VBA帮助里关于Shape对象top属性的说明就知道缺省情况下top属性是相对于anchor来计算的。以下是英文的:

Top Property
See AlsoApplies ToExampleSpecificsTop property as it applies to the Shape and ShapeRange objects.

Returns or sets the vertical position of the specified shape or shape range in points. Read/write Single.

expression.Top
expression Required. An expression that returns one of the above objects.

Remarks
The position of a shape is measured from the upper-left corner of the shape's bounding box to the shape's anchor. The RelativeVerticalPosition property controls whether the shape's anchor is positioned alongside the line, the paragraph, the margin, or the edge of the page.

For a ShapeRange object that contains more than one shape, the Top property sets the vertical position of each shape.

如果你不知道anchor是什么,点击显示/隐藏按钮,再选中一个Shape, 在左边的页边距区域里就会出现一个像锚的东西。那个就是你选中shape的anchor。简而言之,anchor是shape与主文档的联结点。删除anchor所在的段落就可以删除shape。

现在回到你的问题。你移动不到同一位置的原因是两个矩形的anchor在不同的位置。解决办法有二:
1)在添加矩形时就指定anchor,这一般通过指定AddShape等等函数的最后一个参数做到,例如:
activedocument.Shapes.AddShape msoShapeRectangle, 100, 100, 100, 100, selection.Range '指定anchor位置为当前光标位置

2) 设定所有Shape的RelatetiveVerticalPosition属性为相对页边之后再存取Shape的位置。
activedocument.Shapes(1).RelativeVerticalPosition = wdRelativeVerticalPositionPage
activedocument.Shapes(2).RelativeVerticalPosition = wdRelativeVerticalPositionPage
activedocument.Shapes(2).top = activedocument.Shapes(1).top
熊孩子开学喽 2004-07-26
  • 打赏
  • 举报
回复
不懂,我就顶!

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧