I want to resize a selected picture using VBA but I only want to
change the width of the picture to say, 2cm and the height should
adjust to scale automatically.

Manually, using the Format>Picture command I can achieve this by
ensuring that Lock aspect ratio checkbox is True.

However, if I translate this to VBA using the following commands:
with Selection
.InlineShapes(1).LockAspectRatio = msoTrue
.InlineShapes(1).Width = CentimetersToPoints(2)
End With
...I would expect the same to happen as when I did it manually. Even
though the Lock aspect ratio is set to True it reduces the width to
2cm but leaves the height at its original value.

Am I using the correct commands, please?

Re: Resizing pictures with VBA by Karl

Karl
Tue Feb 13 18:51:34 CST 2007

Roderick O'Regan <rory@removethis.theoregans.com> wrote:
> I want to resize a selected picture using VBA but I only want to
> change the width of the picture to say, 2cm and the height should
> adjust to scale automatically.
>
> Manually, using the Format>Picture command I can achieve this by
> ensuring that Lock aspect ratio checkbox is True.
>
> However, if I translate this to VBA using the following commands:
> with Selection
> .InlineShapes(1).LockAspectRatio = msoTrue
> .InlineShapes(1).Width = CentimetersToPoints(2)
> End With
> ...I would expect the same to happen as when I did it manually. Even
> though the Lock aspect ratio is set to True it reduces the width to
> 2cm but leaves the height at its original value.
>
> Am I using the correct commands, please?

Maybe you need to actually do the math, and set the height correspondingly?
--
.NET: It's About Trust!
http://vfred.mvps.org



Re: Resizing pictures with VBA by Jay

Jay
Tue Feb 13 20:38:38 CST 2007

Somewhere in the bowels of the VBA interpreter, the code that handles
.LockAspectRatio is broken. What works in the dialog doesn't work at
all in VBA.

Here's some code from one of my macros to handle proportional
resizing.

' change these numbers to the maximum width and height
' (in inches) to make the inserted pictures
Const PicWidth = 1.9
Const PicHeight = 2.25

Dim Photo As InlineShape

Set Photo = .InlineShapes.AddPicture(FileName:=FName, _
LinkToFile:=False, SaveWithDocument:=True, _
Range:=PicRg)
With Photo
RatioW = CSng(InchesToPoints(PicWidth)) / .Width
RatioH = CSng(InchesToPoints(PicHeight)) / .Height

' choose the smaller ratio
If RatioW < RatioH Then
RatioUse = RatioW
Else
RatioUse = RatioH
End If

' size the picture to fit the cell
.Height = .Height * RatioUse
.Width = .Width * RatioUse
End With

--
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.

On Wed, 14 Feb 2007 00:31:13 +0000, Roderick O'Regan
<rory@removethis.theoregans.com> wrote:

>I want to resize a selected picture using VBA but I only want to
>change the width of the picture to say, 2cm and the height should
>adjust to scale automatically.
>
>Manually, using the Format>Picture command I can achieve this by
>ensuring that Lock aspect ratio checkbox is True.
>
>However, if I translate this to VBA using the following commands:
> with Selection
> .InlineShapes(1).LockAspectRatio = msoTrue
> .InlineShapes(1).Width = CentimetersToPoints(2)
> End With
>...I would expect the same to happen as when I did it manually. Even
>though the Lock aspect ratio is set to True it reduces the width to
>2cm but leaves the height at its original value.
>
>Am I using the correct commands, please?

Re: Resizing pictures with VBA by Roderick

Roderick
Wed Feb 14 17:13:31 CST 2007

Thanks Jay for the help.

Just some small adjustments to fit my circumstances and it works a
treat!

I would never have known about the code in the Interpreter being
broken.

Regards

Roderick


On Tue, 13 Feb 2007 21:38:38 -0500, Jay Freedman
<jay.freedman@verizon.net> wrote:

>Somewhere in the bowels of the VBA interpreter, the code that handles
>.LockAspectRatio is broken. What works in the dialog doesn't work at
>all in VBA.
>
>Here's some code from one of my macros to handle proportional
>resizing.
>
> ' change these numbers to the maximum width and height
> ' (in inches) to make the inserted pictures
> Const PicWidth = 1.9
> Const PicHeight = 2.25
>
> Dim Photo As InlineShape
>
> Set Photo = .InlineShapes.AddPicture(FileName:=FName, _
> LinkToFile:=False, SaveWithDocument:=True, _
> Range:=PicRg)
> With Photo
> RatioW = CSng(InchesToPoints(PicWidth)) / .Width
> RatioH = CSng(InchesToPoints(PicHeight)) / .Height
>
> ' choose the smaller ratio
> If RatioW < RatioH Then
> RatioUse = RatioW
> Else
> RatioUse = RatioH
> End If
>
> ' size the picture to fit the cell
> .Height = .Height * RatioUse
> .Width = .Width * RatioUse
> End With

Re: Resizing pictures with VBA by Karl

Karl
Wed Feb 14 18:12:52 CST 2007

Roderick O'Regan <rory@removethis.theoregans.com> wrote:
> Thanks Jay for the help.
>
> Just some small adjustments to fit my circumstances and it works a
> treat!
>
> I would never have known about the code in the Interpreter being
> broken.

I'm not sure it's right to say it's "broken." You're just given full control. What
you do with it may even surprise you, but it seems to me it's just executing
instructions as you enter them.
--
.NET: It's About Trust!
http://vfred.mvps.org



Re: Resizing pictures with VBA by Jay

Jay
Wed Feb 14 21:47:11 CST 2007

On Wed, 14 Feb 2007 16:12:52 -0800, "Karl E. Peterson" <karl@mvps.org>
wrote:

>Roderick O'Regan <rory@removethis.theoregans.com> wrote:
>> Thanks Jay for the help.
>>
>> Just some small adjustments to fit my circumstances and it works a
>> treat!
>>
>> I would never have known about the code in the Interpreter being
>> broken.
>
>I'm not sure it's right to say it's "broken." You're just given full control. What
>you do with it may even surprise you, but it seems to me it's just executing
>instructions as you enter them.
>--
>.NET: It's About Trust!
> http://vfred.mvps.org
>

Hi Karl,

I'd say it's broken. If you explicitly set the .LockAspectRatio
parameter to True, you have every right to expect that setting one
dimension of the picture will adjust the other dimension to... *lock*
the damn aspect ratio! You don't expect to have to fiddle around with
floating point math and maintain your own variables. If you want to do
those things, then you can set .LockAspectRatio to False, or ignore it
and let it take its default value of False.

--
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.

Re: Resizing pictures with VBA by Karl

Karl
Thu Feb 15 15:17:53 CST 2007

Jay Freedman <jay.freedman@verizon.net> wrote:
> I'd say it's broken. If you explicitly set the .LockAspectRatio
> parameter to True, you have every right to expect that setting one
> dimension of the picture will adjust the other dimension to... *lock*
> the damn aspect ratio! You don't expect to have to fiddle around with
> floating point math and maintain your own variables. If you want to do
> those things, then you can set .LockAspectRatio to False, or ignore it
> and let it take its default value of False.

Well, you can say _whatever_ you want, it doesn't change one simple fact: If you
rely on assumptions and expectations, as a developer, you will spend many
frustrating hours banging your head against the wall and pulling out your own hair.
If you actually write you code to *do* what you want it to do, and not rely on any
sort of default behavior, you spend far less time wondering why the code isn't doing
what you _want_ it to do (especially when it is, in fact, doing what you *told* it
to do! <g>). Note I'm not saying your expectations are unreasonable. The only
thing that's unreasonable here is the expectation that a compiler will act on your
expectations rather than what it's actually provided code to do. These are
machines, afterall. :-)
--
.NET: It's About Trust!
http://vfred.mvps.org