none
How do i Resize a Picture (enchanced Metafile) after a Selection.PasteSpecial operation in a macro (word 2007) RRS feed

  • Question


  • Hi there,

    I could really do with some help on this one, After exhaustive searching for 2 hours I still cannot find the solution. Basically I have lots of Excel Tables i need to paste into a report, and I have a Macro that will paste these as Enhanced Metafiles. But I would also like to resize these pictures in the same function.

    The problem is that the "Selection.PasteSpecial" function does not return an object identifier for me to handle, using the following line will not work either because the object that is pasted is not currently selected.

    Selection.InlineShapes(1).Width = CentimetersToPoints(9)

    the line will return "The Requested member of the collection does not exist".

    I found a way to index the number of InlineShapes in the document, but again, this will only work if its the last one in the document, so its not referencing the correct Picture.

    Sub pasteEnchMeta()
    ' pasteEnchMeta Macro
    Dim PasteObect As InlineShape
    Dim Count As Integer     
    
    Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _Placement:=wdInLine, DisplayAsIcon:=False   
    
    Count = ActiveDocument.InlineShapes.Count   
    Set PasteObect = ActiveDocument.Content.InlineShapes(Count)   
    
    With PasteObect   
    .Width = CentimetersToPoints(9)   
    End With   
    
    End Sub


    What can I do to be able to resize the Enhanced metafile that I have just pasted?

    Any help would be very much appreciated.




    • Edited by Ribbley Wednesday, August 1, 2012 1:10 PM more spelling
    Wednesday, August 1, 2012 1:08 PM

All replies

  • How you are using selection.

    I am not so confident with word object model.But I think word indexes by position in document.If you constantly go downwards/upawards then I think it is possible in your code.

    But if your selection is random then it is probably not doable.


    Asadulla Javed

    Wednesday, August 1, 2012 2:51 PM
    Answerer

  • Hello,

    Interestingly the above code will work but only if its the last picture in the document, you are correct that word indexes the pictures via InlineShapes(n). Your comment that its not doable would seem consistent with the fact there are no workable examples on the internet.  But it seems crazy that when pasting an object into a document the pasting process does not return a reference, like a function would. PasteSpecial seems to be unable to do this, but their must be a way around it. 

    I tried using the PastSpecial function with parenthesis format also, like so:

    set PasteObect = Selection.PasteSpecial(Link:=False, DataType:=wdPasteEnhancedMetafile, _
            Placement:=wdInLine, DisplayAsIcon:=False)

    but this is also not valid syntax, because it returns the error "Expected function or variable". Again confirming PasteSpecial doesnt return anything.



    • Edited by Ribbley Wednesday, August 1, 2012 3:40 PM typo
    Wednesday, August 1, 2012 3:37 PM
  • "I am not so confident with word object model".Today only I found a solution for you.Actually Inlineshapes are treated as charecters hence indexed as per position like charecters.

    But if you paste as shape change the width and convert to inlineshape then it's doable.

    Because shape object is indexed with the order of entry into documents.

    Sub pasteEnchasaMeta()
        Dim PasteObect As Shape
        'changed to shape
        Dim Count As Integer
        
        Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
            Placement:=wdFloatOverText, DisplayAsIcon:=False
            'Changed Placement for pasting as shape
        
        Count = ActiveDocument.Shapes.Count
        Set PasteObect = ActiveDocument.Shapes(Count)
        
        With PasteObect
            .LockAspectRatio = msoFalse
            'This is required otherwise shape's height will be changed
            .Width = CentimetersToPoints(9)
            .ConvertToInlineShape
            'Converted to inline shape
        End With
    End Sub

    ---------------------------------------------------------------------------------------------

    Please do not forget to click “Vote as Helpful” if any post helps you and Mark as Answer if it solves the issue.



    Asadulla Javed

    • Proposed as answer by Al Silva Friday, April 26, 2013 3:25 PM
    Thursday, August 2, 2012 6:05 AM
    Answerer
  • Oh wow, Thanks so much. Sorry I didnt reply sooner, i was away from my computer.

    This looks like it gets around that sticky problem with the way Word references the object. I will implement it and let you know.

    br

    Paul

    Monday, August 6, 2012 2:02 PM
  • Good news, it works wonders.

    I have now set .LockApsectRatio = true. so that the scaling is preserved.

    Thanks very much, you help is greatly appreicated. For anyone else reading this, I can recommend linking a macro like htis to a Short-cut key command, so its really simple to copy and paste anything into a document as an enchanced Metafile, also pasting them while contained within a 1-cell table also is a great way to Anchor it in place.


    • Edited by Ribbley Monday, August 6, 2012 3:26 PM type
    Monday, August 6, 2012 3:26 PM
  • It's good to know that the effort helped.

    May I request you to mark it as answer ? This will help other's to solve the similar issue quickly and effectively.


    Best Regards,
    Asadulla Javed, Kolkata
    ---------------------------------------------------------------------------------------------
    Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it solves the issue.

    Friday, August 17, 2012 1:50 PM
    Answerer
  • "I am not so confident with word object model".Today only I found a solution for you.Actually Inlineshapes are treated as charecters hence indexed as per position like charecters.

    But if you paste as shape change the width and convert to inlineshape then it's doable.

    Because shape object is indexed with the order of entry into documents.

    Sub pasteEnchasaMeta()
        Dim PasteObect As Shape
        'changed to shape
        Dim Count As Integer
        
        Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
            Placement:=wdFloatOverText, DisplayAsIcon:=False
            'Changed Placement for pasting as shape
        
        Count = ActiveDocument.Shapes.Count
        Set PasteObect = ActiveDocument.Shapes(Count)
        
        With PasteObect
            .LockAspectRatio = msoFalse
            'This is required otherwise shape's height will be changed
            .Width = CentimetersToPoints(9)
            .ConvertToInlineShape
            'Converted to inline shape
        End With
    End Sub

    ---------------------------------------------------------------------------------------------

    Please do not forget to click “Vote as Helpful” if any post helps you and Mark as Answer if it solves the issue.



    Asadulla Javed

    This was really helpful in getting past the roadblock. Thx
    Friday, April 26, 2013 3:26 PM