PDA

View Full Version : Paste and resize image from clipboard macro help



ElJamoquio
09-09-2017, 10:16 AM
Hello, and apologies! I'm a complete newbie to VBA.

I'm trying to create a macro to paste and automatically resize an image that's on my clipboard. I've found similar macros on the web, but as a complete newbie to VBA I'm not able to do what I want.

Here's one example:

On Error Resume Next
Dim oShp As Shape
Dim iShp As InlineShape
Dim ShpScale As Double
'Selection.Paste (here I tried to paste it, didn't seem to work)
With Selection
For Each iShp In .InlineShapes
With iShp
If .Type = wdInlineShapePicture Or wdInlineShapeLinkedPicture Then
ShpScale = InchesToPoints(4) / .Width
.Width = .Width * ShpScale
.Height = .Height * ShpScale
End If
End With
Next iShp
End With





Can anybody help? Why wasn't the paste working in the code above? What is recommended - just want to paste in an image with a certain size, seems like that should be relatively straightforward.

gmaxey
09-09-2017, 06:34 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/9/2017
Dim oILS As InlineShape
Dim oRng As Range
Dim dblScale As Double
Set oRng = Selection.Range
oRng.Paste
If oRng.InlineShapes.Count = 1 Then
Set oILS = oRng.InlineShapes(1)
With oILS
dblScale = InchesToPoints(4) / .Width
.Width = .Width * dblScale
.Height = .Height * dblScale
End With
End If
lbl_Exit:
Exit Sub
End Sub

ElJamoquio
09-09-2017, 07:28 PM
Thanks!

I think I only need to multiply the width, it then keeps the aspect ratio correct. I was wondering why it was doing a resize twice in my code as well.

Thanks again! Perfect.