Creating a macro in word to resize an image

S

Simon

Hi

I am trying to record a macro in Word 2003 which will resize an images width
to 11cm.

The trouble is, the images are all different dimensions, so I need to lock
the aspect ratio for the height.

Whenever I try to record a macro, it always reverts to a fixed height.

Please help.

PS I don't know any VB
 
J

Jay Freedman

Simon said:
Hi

I am trying to record a macro in Word 2003 which will resize an
images width to 11cm.

The trouble is, the images are all different dimensions, so I need to
lock the aspect ratio for the height.

Whenever I try to record a macro, it always reverts to a fixed height.

Please help.

PS I don't know any VB

It will be impossible to create such a macro strictly by recording. Since
you don't know any VB, either you'll have to learn some, or you can work
with us a bit to get what you need.

You don't say whether the images you're resizing are in line with text or
floating, which makes a big difference in the world of VBA. So the following
macro does both kinds, and simply does nothing if there are no images of one
kind or the other. You can simplify the macro if you only use one kind, but
it won't make a big difference if you don't simplify it.

Also, the macro makes all images 11 cm wide. It doesn't have any way to say
"... except this one". If you want the macro to apply only to the one
specific image that's currently selected, that will take a different macro
(not too different, but...). If you change your mind about the size, that's
easy: change the number in the parentheses after CentimetersToPoints in four
places.

Sub ResizeAllImages()
' make all images (both inline and floating)
' 11 cm wide while preserving aspect ratio

Dim oShp As Shape
Dim oILShp As InlineShape

For Each oShp In ActiveDocument.Shapes
With oShp
.Height = AspectHt(.Width, .Height, _
CentimetersToPoints(11))
.Width = CentimetersToPoints(11)
End With
Next

For Each oILShp In ActiveDocument.InlineShapes
With oILShp
.Height = AspectHt(.Width, .Height, _
CentimetersToPoints(11))
.Width = CentimetersToPoints(11)
End With
Next
End Sub

Private Function AspectHt( _
origWd As Long, origHt As Long, _
newWd As Long) As Long
If origWd <> 0 Then
AspectHt = (CSng(origHt) / CSng(origWd)) * newWd
Else
AspectHt = 0
End If
End Function

Post back if this doesn't solve the problem.

If you decide you want to learn enough to modify the macro on your own, read
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
S

Simon

Jay thanks for your reply

Just to qualify exactly what I need:

1. All images are inline with text

2. I don't actually want the macro to run on all images in the document, I
only want to run it on an image I have selected (some are less than 11cm
that I want to keep that way plus I need to check each image individually).

Really appreciate the help

Simon
 
J

Jay Freedman

Simon said:
Jay thanks for your reply

Just to qualify exactly what I need:

1. All images are inline with text

2. I don't actually want the macro to run on all images in the
document, I only want to run it on an image I have selected (some are
less than 11cm that I want to keep that way plus I need to check each
image individually).
Really appreciate the help

Simon
OK, here's a modified version that handles only inline images, and only
those that are selected -- you can select a chunk of text that includes more
than one image and the macro will change them all.

Sub ResizeSelectedImage()
' make selected inline images
' 11 cm wide while preserving aspect ratio

Dim oILShp As InlineShape

For Each oILShp In Selection.InlineShapes
With oILShp
.Height = AspectHt(.Width, .Height, _
CentimetersToPoints(11))
.Width = CentimetersToPoints(11)
End With
Next
End Sub

Private Function AspectHt( _
origWd As Long, origHt As Long, _
newWd As Long) As Long
If origWd <> 0 Then
AspectHt = (CSng(origHt) / CSng(origWd)) * newWd
Else
AspectHt = 0
End If
End Function

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top