Copy shape in Word 2010 without .Select?

给你一囗甜甜゛ 提交于 2020-01-23 10:55:12

问题


Is it possible to copy a shape in Word 2010 without resorting to .Select? According to Dev Center the Anchor property returns the shape's anchoring range. Could that be a way forward? However, the code below returns an error.

Sub createShape()
    Set myShape = ActiveDocument.Shapes.AddShape(msoShapeRectangle, 1, 1, 1, 1)
    myShape.Anchor.Copy
End Sub

回答1:


While it does not seem to be possible to copy a shape without selecting it, it is possible to duplicate a shape without selecting it (which was my reason for wanting to copy it in the first place). The code below gives me what I was looking for:

Sub createShape()
    Set myshape = ActiveDocument.Shapes.AddShape(msoShapeRectangle, 100, 100, 100, 100)
    Set anothershape = myshape.Duplicate
End Sub



回答2:


If you've got what you're looking for then thats great, but you can copy a shape as such by copying the paragraph (or range rather) that the shape is anchored to. For example:

Sub createShape()
   Dim myShape As Shape, myRange As Range

   Set myShape = ActiveDocument.Shapes.AddShape(msoShapeRectangle, 10, 10, 10, 10)
   Set myRange = myShape.Anchor.Paragraphs(1).Range
   myRange.Copy
End Sub

The trouble with this however is that it will copy any text in the paragraph that you have anchored it to or your anchor maybe in a table which can cause strange things to happen.

You can also change the shape to an inline shape after inserting it so it fits with the text and has more obvious range as anchor points have a habit of moving around and being generally unpredictable.



来源:https://stackoverflow.com/questions/14816041/copy-shape-in-word-2010-without-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!