Scaling Pictures with VBA

A

aehan

Hello

I contacted Discussion Groups a few weeks ago because I wanted to scale
imported pictures to 50%, 25%, 75% etc of their size using code, and Jean-Guy
Marcil gave me some code that I really appreciated. However, I have hit a
snag. The pictures are logos, and are pictures and text, when Word imports
them it fits them across the margins, changing the size of the logo which
will now appear as, eg 55% of the original. When I wrote the routine for
scaling the macro I took this into account, eg 50% was half of 55% and so on.
However, to my horror I now see that Word uses the existing margins and
orientation of the current document, so what was 55% in portrait changes when
you import it as landscape.

Help!!! Does anyone have an idea of how I can write the code to look at the
proprotions of the graphic as displayed in the height and width boxes of the
Size properties in the Format dialogue box? Hope I am explaining this
correctly.

The code as provided by Jean-Guy at present is:

With Selection
Select Case .Type
Case 7 'Inline shape
.InlineShapes(1).ScaleHeight = 50
.InlineShapes(1).ScaleWidth = 50

Case 8 'Floating picture
.ShapeRange(1).ScaleHeight 0.5, True
.ShapeRange(1).ScaleWidth 0.5, True

Case Else
MsgBox "The current selection is not a picture." _
& vbCrLf & "Make sure you select only the picture itself.",
_
vbExclamation, "Error"
End Select
End With

Thanks for any help you can give.

Aehan
 
J

Jean-Guy Marcil

aehan was telling us:
aehan nous racontait que :
Hello

I contacted Discussion Groups a few weeks ago because I wanted to
scale imported pictures to 50%, 25%, 75% etc of their size using
code, and Jean-Guy Marcil gave me some code that I really
appreciated. However, I have hit a snag. The pictures are logos,
and are pictures and text, when Word imports them it fits them across
the margins, changing the size of the logo which will now appear as,
eg 55% of the original. When I wrote the routine for scaling the
macro I took this into account, eg 50% was half of 55% and so on.
However, to my horror I now see that Word uses the existing margins
and orientation of the current document, so what was 55% in portrait
changes when you import it as landscape.

Modify your code to incorporte the following (assuming you want a 1-inch
wide logo):

'72 points = 1 inch
Dim PixWidth As Long

With Selection.InlineShapes(1)
PixWidth = .Width
.Width = 72
.Height = .Height * (72 / PixWidth) 'to keep the aspect ratio
End With

or

Selection.ShapeRange(1).Width = 72

(Inline shapes do not keep the ratio, floating ones will if that checkbox is
on - Format > Object > Size tab - , which it is by default)

It is better to work with absolutes rather than relative numbers (%) when
the context in unpredictable.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

aehan

Thankyou again for all your help. I'm going to give it a whirl. Some of the
people sizing the graphics could have their rulers set to cm, some mm and
some inches - they're so fickle - but I'll tell them to stick to cm and lump
it (that's the default here).

Thanks!!!
 
J

Jay Freedman

The sizing in Word works in points (as Jean-Guy showed in his code).
You can use the functions InchesToPoints() and CentimetersToPoints()
to do conversions if needed. You can ask the user to specify what unit
their requested size is expressed in, or you can look at the value of
Options.MeasurementUnit to see what they have set and just assume
that's the unit for their request.
 
J

Jean-Guy Marcil

aehan was telling us:
aehan nous racontait que :
Thankyou again for all your help. I'm going to give it a whirl.
Some of the people sizing the graphics could have their rulers set to
cm, some mm and some inches - they're so fickle - but I'll tell them
to stick to cm and lump it (that's the default here).

And to add to what Jay wrote, if you decide that the space allowed for the
graphic on the page is one inch wide, then it does not matter what the units
on the user's ruler are, the code will work regardless.

This would only be an issue if users were using the ruler to gauge the space
they want to use and that your code was interactive.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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