Insert picture, resize to specific width, keeping height proportio

G

Guest

Hi,

My sub will insert a picture into my template and resize the width to 240.
The original picture aspect ratio will vary, so I will not know the resize
height ahead of time. I have written the below sub all by myself so far, so
there's a good chance there's something wrong with it. It resizes the height
proportionately to the width, then places the bottom left corner of the
picture at the bottom left corner of cell K11.

Dim PicHt As Double
Dim PcWd As Double
Dim PicRatio As Double

PRM.Activate
PRM.Cells(12, 11).Select
PRM.Pictures.Insert "C:\Documents and Settings\Olab\My Documents\0- Wear
Testing\Sketches\" & sStyle & ".jpg"
With PRM.Pictures("Picture 8")
PicHt = .Height
PicWd = .Width
PicRatio = PicHt / PicWd
.Select
.Width = 240
.Height = PicHt * PicRatio
.Top = .Top - .Height + 1
End With

It seems to work so far, but I have two specific questions and if you see
anything else I could do better, please let me know.

1. Is Double the correct data type to use? I know Long doesn't work because
it's integers only. I tried Decimal, but my vba doesn't recognize this data
type. All I need is for it to go to 2 decimal points. I'm not exactly sure
what Double means, even after reading the Help definition. What data type is
best for this?

2. Is there a way to specify the picture to manipulate as the last picture
inserted instead of "Picture 8"? I tried using this method I found in a post:

Dim Pic as Picture
Dim sStyle As String

sStyle = PRM.Cells(5, 2).Text
Set Pic = PRM.Pictures.Insert "C:\Documents and Settings\Olab\My
Documents\0- Wear Testing\Sketches\" & sStyle & ".jpg"
Pic.Selet

But an error states that the quotation mark after "Sketches\" should be the
end of statement. It does work with a normal file path, but I think my
variable is throwing it off.

Thank you!

Thanks!
 
D

Dave Peterson

#1. I'd use As Double because I want to include the decimal portion of the
number.

#2.
Set Pic = PRM.Pictures.Insert("C:\Documents and Settings\Olab\" & _
"My Documents\0- Wear Testing\Sketches\" & sStyle & ".jpg")

(notice the extra ()'s.)
 
G

Guest

Thanks, Dave! Your suggestion certainly worked! It was my bad...parens
were in original post by Tom O. BTW, I know Double works, but I'm not
exactly sure of all the different data types that allow decimal points. I
read the help definitions, but I'm still not sure. Could you please explain
in lay terms the difference between the different data types that allow
decimals?

thank you!!!
 
G

Guest

if you search for "data type summary" in VBA-Help you get a good overview.

Double is an 8-byte floating number, which works good in your case. You
could also use Single (4-byte floating number) which is smaller, but I don't
know if it would change a lot in your project.

hth

Carlo
 
G

Guest

Hi Carlo,

thank you for your reply, but like I said, I already read the vba help
summary and I really don't understand the terms used like "floating point",
etc. etc.

I was hoping for someone could explain in lay terms the difference between
the different data types that allow decimals.



p.s. in case anyone is pays attention to my picture resizing sub where you
only know the width, I caught a boo boo and here's the revised sub:

SUMFB.Activate
SUMFB.Cells(15, 16).Select '''VARIABLE PRM
Set pSketch = SUMFB.Pictures.Insert("C:\Documents and Settings\Olab\My
Documents\0- Wear Testing\Sketches\" & sStyle & ".jpg") '''VARIABLE PRM
With pSketch
PicHt = .Height
PicWd = .Width
.Select
.Width = 240
PicRatio = .Width / PicWd
.Height = PicHt * PicRatio
.Top = .Top - .Height + 1
End With
 
G

Guest

Floating Point means decimal point.

So, if you want to use decimal point you can choose between:
double and single

if you want to have whole numbers you can choose between all the rest
(including single and double)

Hope that I spoke in lay terms, as I am also a lay ;)

For your Resize sub you can shorten it to:
----------------------------------------------------------
SUMFB.Activate
'i'm not sure, if you really need to select this cell!
SUMFB.Cells(15, 16).Select '''VARIABLE PRM
Set pSketch = SUMFB.Pictures.Insert("C:\Documents and Settings\Olab\My
Documents\0- Wear Testing\Sketches\" & sStyle & ".jpg") '''VARIABLE PRM
With pSketch
PicRatio = .Width / .Height
.Width = 240
.Height = .Width * PicRatio
'not sure, what you want to achieve with the following line
.Top = .Top - .Height + 1
End With
 
G

Guest

Thanks Carlo for the explanation and for the code tightening. I need that
last line because it is part of how I position the picture by lining up the
lower left corner to the lower left corner of a cell.

:)
 
D

Dave D-C

Floating Point means decimal point.
A bit of a simplication.

More than you want to know?:
http://en.wikipedia.org/wiki/Floating_point

A waste of time (but funny?)
http://del.icio.us/url/53db119a950dc8c8b8dfbd087cf4680e

Ignore this if it makes things more confusing:

(This ignores negative numbers)
(This uses small numbers just for explanation)
If you have a 3-digit field, three options for storing numbers:

1) Integer:
You can store 0 to 999.
You can store only the integer part of 1.2
You can't store 12000 nor .00034
You can't store 12345 nor .00056789.

2) Fixed (decimal) Point, say 2 decimal places.
You can store 0.00 to 9.99.

3) Floating Point:
Allows you to store a very large range of numbers
but at 2-digit accuracy instead of 3.

The first 2 digits will be the "mantissa", the first 2
significant digits, which will range from .00 to .99
The 3rd digit will be 4 + the exponent of 10.
The exponent can range from -4 to 5.
The equation of the value is
mantissa * 10 ^ (3rd digit - 4).
("^" means "to the power of")
You will be able to
store 0 to 99.
store 1.2 and 34000 and .00056.
store 999 and 12345 and .00056789, but not exactly.

1 stores as 105 (.10*10^1=1)
1.2 stores as 125 (.12*10^1=1.2)
10 stores as 106 (.10*10^2=10)
99 stores as 996 (.99*10^2=99)
100 stores as 107 (.10*10^3=100)
230 stores as 237 (.23*10^3=230)
1000 stores as 108 (.10*10^4=1000)
34000 stores as 349 (.34*10^5=34000)
..1 stores as 104 (.10*10^0=.1)
..00056 stores as 561 (.56*10^-3=.00056)
999 stores as 997 (.99*10^3=990) not exact
12345 stores as 129 (12*10^5=12000) not exact
..00056789 stores as 561 (.56*10^-3=.00056) not exact

In binary it's a binary point instead of a decimal point,
but the idea is the same. But .8 in binary is
..110011001100..(infinitely) as 1/3 in decimal is
..3333..(inifinitely); and there is rounding,
so there are comparing issues in floating point.

D-C Dave
 

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