Trouble pasting then picking up shape using VBA

Fi69
Fi69 used Ask the Experts™
on
Hi experts

I am trying to paste a shape (copied from a PDF document) and then pick it up and do stuff with it. Trouble is when I paste the image using the code below it doesn't pick it up.

Is there a way to declare the shape on paste?

For instance: Set oshp = selection.pastespecial placement:=wdfloatovertext
(this code errors by the way)

Dim oshp As Shape
     Selection.PasteSpecial placement:=wdFloatOverText
     
     Set oshp = Selection.ShapeRange(1)
     oshp.ConvertToInlineShape
     'go do something with oshp

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Top Expert 2012
Commented:
Hi Fi,

I expect that you were only doing the conversion as a way of trying to get round your problem of capturing the image into an object variable. However if you do need to do that at any time, set the new object from the convert method.

Set oILshp =  oshp.ConvertToInlineShape


For your actual problem, depending on whether you want an inline or a floating object, you would need one of the macros below

Sub PasteInLine()
    Dim oILshp As InlineShape
    
    Selection.PasteSpecial Placement:=wdInLine
    'move selection start back to encompass the shape
    Selection.MoveStart wdCharacter, -1
    Set oILshp = Selection.InlineShapes(1)
    'go do something with oshp
    Debug.Print oILshp.Height
End Sub

Sub PasteShape()
    Dim oshp As Shape
    
    Selection.PasteSpecial Placement:=wdFloatOverText
    'shapes are anchored to paragraph ranges.
    Set oshp = Selection.Paragraphs(1).Range.ShapeRange(1)
    Debug.Print oshp.Left
    'go do something with oshp
End Sub

Open in new window

Author

Commented:
Hi Graham

Thanks so much for that. I've used the PasteInLine sub.  I wonder why I didn't use that in the first place...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial