Changing image size proportionately

L

Lighthouse

I have an odd problem that seems like it shouldn't be a problem. Hoping
someone can help me with this.

I have some Word documents that may have images that need to be sized
programmatically. These docs are generated into Word through a
conversion from another application. The original application does not
have a reliable method of scaling the images. Consequently, some images
arrive in Word as HUGE! I noticed in Word that I could search for a
graphic (^g), open the "Format picture" dialog, click the "Size" tab,
then set the "width" to 6 inches while making sure that "Lock Aspect
Ratio" is checked.

Seemed like a good candidate for a macro, especially since I want to do
this automatically as part of the conversion process.

The problem is that I can not seem to resize these pictures
proportionately through VBA like I was able to do it manually. This
does not seem right since my understanding is that I should have MORE
control over my document w/ VBA rather than LESS.

When I recorded my actions as a macro then examined the code it showed
my height and width expressed as a no. of pixels. Since I want to limit
my images to 6 inches wide, 432 pixels is perfect (6 in. * 72 dpi).
However, I don't know the original dimensions of the images. I only
know that I don't want them displayed any wider than 6 inches.

Here is my code:

Public Sub testfind()
x = 0


g = ActiveDocument.InlineShapes.Count

For i = 1 To g
ActiveDocument.InlineShapes(i).Select
If Selection.InlineShapes(i).Width > 432# Then

Selection.InlineShapes(i).Line.Visible = msoFalse
Selection.InlineShapes(i).LockAspectRatio = msoTrue
Selection.InlineShapes(i).Width = 432
End If

Next i

End Sub

When I step through this w/ F8 it sets "g" to 58 and goes through the
IF statement once fine. When "i" is 2 the macro fails on the IF
statement. I get an error message that says: "The requested member of
the collection does not exist." This doesn't seem right since there are
58 members of the collection. Why does it fail on no. 2?

When I go to check InlineShape(1), which is >432, I find that the
height is still at the original dimension, even though I have
LockAspectRatio marked as checked. What is the magic incantation to
make the height change proportionately to the width that I've
specified? TIA
 
K

Klaus Linke

Hi,

It's pretty simple: You select the shape i (i=1, 2, ...) and then use Selection.InlineShapes(i).
Since you've selected one InlineShape, you always get an error except when i=1.

I don't really know why ".LockAspectRatio = msoTrue" doesn't work as you would expect it.
Something like the following seems to work:

Dim myIS As InlineShape

For Each myIS In ActiveDocument.InlineShapes
With myIS
If .Width > InchesToPoints(6) Then
.Width = InchesToPoints(6)
.ScaleHeight = .ScaleWidth
End If
End With
Next myIS

Regards,
Klaus
 
L

Lighthouse

Klaus, thanks very much. This worked EXACTLY as I had hoped. you saved
me much time and frustration. I didn't even know about the
"InchesToPoints() thing.

Lighthouse
 

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