Hi there, wonder if anyone can help me. I'm a bit of a macro amateur....
I have thousands of images that I wish to resize to 210mm wide.
I'm trying this but it's not working:
-Run Temporary Macro
-CTRL A
-CTRL G
-Then I type 210 into the 'width' box
- P (just to centralise it on the page)
-CTRL S
- Close page
- Stop recording
Then I load the images and press CTRL + SHIFT + P to run the temporary macro, it runs, closes the image down but when I go back and check it, it hasn't re-sized it!
Can anyone let me know please what I am doing wrong?
Thanks,
Mark
If you want to see what's going on with macros, you will want to record in such a way that you can look at the code.
If I do the same thing, but record it using "Start recording" rather than "Record Temporary Macro", I get something that is saved. That means that I can use it again later. It also means that I can look at the code in the VBA editor.
Doing it the same way that you are doing, I see this:
Sub Macro5() ' Recorded 2017-08-14 ActivePage.Shapes.All.CreateSelection Dim s1 As Shape Set s1 = ActiveSelection.Group With s1 .RotationCenterX = 38.715232 .RotationCenterY = 55.839693 End With ActiveDocument.ReferencePoint = cdrCenter s1.SetSize 8.267717, 9.602063 ActiveDocument.Save ActiveDocument.CloseEnd Sub
So, it didn't really record anything like, "resize this such that the width becomes 210 mm, while keeping the selection proportional". Instead, it only recorded the width and height to apply to that specific selection to achieve a 210 mm width and keep things proportional.
Run that macro on something else, and it will do something - but it always resizes everything to 8.267717 x 9.602063 inches.
Hat-Trick Designs said:Thanks for this. Reason I just go for temporary macro is because this code means nothing to me unfortunately! Yeah, so I copied what you've shown above and it just re-sizes it to 210mm but doesn't maintain aspect ratio. And doesn't centre it on the page. How come when I record the macro, I change the width and I have the ratio 'locked', it doesn't reflect that in the macro? Surely if I wanted the picture distorted/stretched, I'd leave it unlocked?! When I'm recording the macro, what do I need to do differently to make it do what I want?
I really, really don't know the ins and outs of recording macros.
I do know a little bit about writing simple VBA macros, however - enough to easily write something that will do what you wanted your recorded macro to do.
The reason I was asking questions about your files is that, depending on how you have things organized, it might be easy to automate it even further, so that a macro could go after all of the documents in a folder automatically (open document, select all, resize proportionately, reposition to center of page, save document, close document).
Here is code that should do what I think you wanted to do using the macro recorder - on a single file, that is already open:
Sub Hat_Trick_01()Dim sr As ShapeRangeDim dblOrigWidth As DoubleDim dblOrigHeight As DoubleDim dblScaleFactor As DoubleConst dblTargetWidth As Double = 8.2677165 'target width, in inches 'set selection range Set sr = ActivePage.Shapes.All 'get width and size of selection range sr.GetSize dblOrigWidth, dblOrigHeight 'calculate scale factor to make the selection match the target width dblScaleFactor = dblTargetWidth / dblOrigWidth 'set the new size of the selection range sr.SetSize dblTargetWidth, dblOrigHeight * dblScaleFactor 'align selection range to page sr.AlignRangeToPage cdrAlignHCenter sr.AlignRangeToPage cdrAlignVCenter ActiveDocument.Save ActiveDocument.Close End Sub