PC Review


Reply
Thread Tools Rate Thread

Creating a macro in word to resize an image

 
 
Simon
Guest
Posts: n/a
 
      4th Feb 2009
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


 
Reply With Quote
 
 
 
 
Jay Freedman
Guest
Posts: n/a
 
      4th Feb 2009
Simon wrote:
> 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/Macros...ordedMacro.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


 
Reply With Quote
 
Simon
Guest
Posts: n/a
 
      4th Feb 2009
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



"Jay Freedman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Simon wrote:
>> 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/Macros...ordedMacro.htm.
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup
> so all may benefit.
>



 
Reply With Quote
 
Jay Freedman
Guest
Posts: n/a
 
      4th Feb 2009
Simon wrote:
> 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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


 
Reply With Quote
 
Simon
Guest
Posts: n/a
 
      4th Feb 2009
Jay you are the man.

I owe you a pint for all the work I'll save here.




"Jay Freedman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Simon wrote:
>> 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 FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup
> so all may benefit.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resize image using macro Gotroots Microsoft Excel Worksheet Functions 1 12th Nov 2009 11:04 PM
Trouble creating viewable background image in Word 2004 for mac Dan the New Word Guy Microsoft Word Document Management 1 24th Jul 2009 09:30 PM
stop creating duplicate image files in word Mack Microsoft Word Document Management 1 28th Jun 2008 05:08 AM
Creating a macro that will resize an image =?Utf-8?B?Um9iZXJ0?= Microsoft Powerpoint 2 27th May 2005 06:34 PM
Macro to Resize Cells for Word Wrap Randy Numbers Microsoft Excel Discussion 7 20th Jul 2004 04:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:17 AM.