HOME MENU SEARCH
Bebas biaya 60x setiap
bulan Open
Ad
Bebas biaya 60x setiap
bulan Open
Ad
Word VBA
Resize
Pictures
Kode Voucher Spesial Untukmu
Stay Active With iStyle.id. 50K Off Code
BERANIBARU50 For All Sports Items. 100% Origi
istyle.id Shop Now
Jul 19, 2015 by azurous in Images
In this article I will explain how
you can use VBA for word to
resize pictures in word. In order
to resize a picture you would
need determine 2 things:
1. Whether the picture is an
inline or oating shape
2. The shape index associated
with the picture
Bebas biaya 60x setiap
bulan Open
Ad
B b bi 60
Bebas biaya 60x
setiap bulan
Paling kesel setiap kali ingat ada
biaya pas mau transfer, tarik tunai
atau topup e-wallet
Open
Note Ad
:
Throughout this article it is
assumed that the picture is inline
and the shape index is 1. The
concepts in this article could also
be applied to oating pictures by
using the Shapes collection.
Contents [hide]
Basics:
Method 1:
Method 2:
LockAspectRatio:
Resize Picture Width:
Basics:
pictures are resized using the
.ScaleHeight and
.ScaleWidth property of their
associated shape container. This
property determines the size in
percent relative to the original
picture size to scale the image.
Example:
The following code resizes the
picture height to 90% of the
original picture height:
InlineShapes.Item(1).ScaleHeig
ht = 90
Example:
The following code resizes the
picture width to 40% of the
original picture width:
Shapes.Item(1).ScaleWidth = 40
The problem with the code above
is that the picture is scaled
relative to its original size. Note
that if not resized by the user,
larger picture are resized by
default upon being inserted in
default upon being inserted in
the word document.
For example take a look at the
gure below:
Upon running the code below we
would expect the height of the
image to be reduced by 30
percent, but instead the image
becomes longer in height:
InlineShapes.Item(1).ScaleHeig
ht = 70
Result:
The reason
for this is
for this is
because the
original
picture size
is almost
twice the
size of the
current
picture. The picture was rescaled
upon inserting into the
document. In the gure below
you can see the image in its full
scale:
–
Method 1:
One method for overcomming
this would be to follow the
algorithm below:
1. Scale the image back to its
original size
2. Get the image height
3. Scale the image back to its
initial size
4. Scale the image with a ratio
The code below does this:
Sub Example1()
Dim lngPercent2Scale As Long
Dim lngOriginalHeight As Long
Dim lngScaledHeight As Long
'percent to resize
lngPercent2Scale = 70
'the height of the scaled
image
lngScaledHeight =
InlineShapes.Item(1).Height
'rescale to original size
InlineShapes Item(1) ScaleHeig
InlineShapes.Item(1).ScaleHeig
ht = 100
'the size of the original
image
lngOriginalHeight =
InlineShapes.Item(1).Height
'rescale image
InlineShapes.Item(1).ScaleHeig
ht = _
lngScaledHeight /
lngOriginalHeight * 100
'resize
InlineShapes.Item(1).ScaleHeig
ht _
= lngPercent2Scale *
lngScaledHeight /
lngOriginalHeight
End Sub
Sponsored Recommended by
Content
The Cost of Real Estate in Miami
Might Surprise You
Real Estate Miami | Sponsored Listing
Apartments for Sale in Banda Aceh
Might Be Cheaper Than You Think
Apartments for sale | Search Ads
Villas For Sale in Dubai Might
Totally Surprise You
Villas for Sale in Dubai | Search Ads
Stair Lifts Might Be Cheaper Than
You Think
Stair Lifts | Search Ads
Sta ts | Sea c ds
The line below gets the picture
height:
lngScaledHeight =
InlineShapes.Item(1).Height
The code below resizes the
picture to its original size and
gets the height of the original
picture:
InlineShapes.Item(1).ScaleHeig
ht = 100
'the size of the original
image
lngOriginalHeight =
InlineShapes.Item(1).Height
The line below resizes the image
back to the initial height:
InlineShapes.Item(1).ScaleHeig
ht = _
lngScaledHeight /
lngOriginalHeight * 100
The line resizes the image
InlineShapes.Item(1).ScaleHeig
ht _
= lngPercent2Scale *
lngScaledHeight /
lngOriginalHeight
Note: There is one problem with
this method. It is the fact that the
image is actually resized to its
original size to gets its original
height . This resizing might mess
up your word document. A better
method would be to copy the
picture to a new document and
rescale it there. This is explained
in the next section.
Method 2:
In this method the following
algorithm is used:
1. Automate a new word
document
2. Copy the picture to the new
document
3. Scale the image back to its
original size
4. Get the image height
5. Close the extra word document
6. Scale the image with a ratio
The code below does this:
Sub
Example2()
Dim objDocument As Document
Dim lngPercent2Scale As Long
Dim lngOriginalHeight As Long
Dim lngScaledHeight As Long
'percent to resize
lngPercent2Scale = 70
Set objDocument =
Documents.Add
InlineShapes.Item(1).Select
Selection.Copy
objDocument.Activate
Selection.Paste
'the height of the scaled
image
lngScaledHeight = _
ActiveDocument.InlineShapes.It
em(1).Height
'rescale to original size
ActiveDocument.InlineShapes.It
em(1).ScaleHeight = _
100
'the size of the original
image
l O i i lH i ht
lngOriginalHeight = _
ActiveDocument.InlineShapes.It
em(1).Height
'close document
objDocument.Close (False)
ThisDocument.Activate
'resize
InlineShapes.Item(1).ScaleHeig
ht _
= lngPercent2Scale *
lngScaledHeight /
lngOriginalHeight
End Sub
The code below automates a new
word document, copies the image
and pastes it in the new
document:
Set objDocument =
Documents.Add
InlineShapes.Item(1).Select
Selection.Copy
objDocument.Activate
Selection.Paste
The rest of the code is similar to
the previous section.
Before:
After:
–
LockAspectRatio:
By default pictures have their
LockAspectRatio set to True. This
will result in the width resizing
when you scale the height and
vice versa. This can be changed
by using the code below:
InlineShapes.Item(1).LockAspec
tRatio = msoFalse
Resize Picture
Width:
This can be done using the code
below:
Sub Example3()
Dim objDocument As Document
Dim lngPercent2Scale As Long
Dim lngOriginalWidth As Long
Dim lngScaledWidth As Long
InlineShapes.Item(1).LockAspec
tRatio = msoFalse
'percent to resize
lngPercent2Scale = 70
Set objDocument =
Documents.Add
InlineShapes.Item(1).Select
Selection.Copy
objDocument.Activate
Selection.Paste
'the height of the scaled
image
lngScaledWidth = _
ActiveDocument.InlineShapes.It
em(1).Width
'rescale to original size
ActiveDocument.InlineShapes.It
em(1).ScaleWidth = _
100
'the size of the original
image
lngOriginalWidth = _
ActiveDocument.InlineShapes.It
em(1).Width
'close document
objDocument.Close (False)
ThisDocument.Activate
'resize
InlineShapes.Item(1).ScaleWidt
h _
= lngPercent2Scale *
lngScaledWidth /
lngOriginalWidth
End Sub
The highlighted text sets the
LockAspectRatio to false. This
will prevent the picture height
from resizing when the width is
resized. The rest of the code is
pretty much the same as the
previous section.
You can download the le and
code related to this article from
the link below:
Resize Pictures.docm
See also:
Word VBA, Picture Format
CropBottom, CropLeft, CropRight
and CropTop
Word VBA, Shape Index
Word VBA, Loop Through
Images
If you need assistance with your code,
or you are looking for a VBA
programmer to hire feel free to contact
me. Also please visit my website
www.software-solutions-online.com
Tagged with: Pictures, Resize, VBA, Word
About the author azurou
“Word VBA
3 thoughts on
Resize Pictures”
Leave a Reply
Your email address will not be published.
Required elds are marked *
Comment
Name *
Email *
Website
Post Comment
Software-Solutions-
Online.com
Copyright 2020 Software-Solutions-Online.com.
All rights reserved.
report this ad
Sponsored Recommended by
Content
Getting a Job in UK may be easier
than you think
Jobs in UK | Sponsored Listing
[?]
Hair Restoration in Banda Aceh
Might Be Way Cheaper Than You…
Hair Transplant Clinic | Search Ads
Real Estate Prices In Las Vegas
Might Surprise You
Las Vegas Real Estate | Search Ads
Getting a Master's Degree in
Germany May Be Easier Than You…
Master Degree in Germany | Sponsored Listi…
report this ad
RECENT POSTS
IF
How Nested Ifs In VBA Work with
Examples
0 COMMENTS
CC SS
ACCESS
Docmd.RunSQL in Access VBA
0 COMMENTS
EXCEL
How to Use IfError in VBA and
Excel
0 COMMENTS
OPERATORS
Using VBA Like to Compare
Strings
0 COMMENTS
EXCEL
How to Use VBA to Check if a Cell
is Empty in MS Excel
0 COMMENTS
POPULAR POSTS
FILE AND FOLDERS
1 Find and List all Files
and Folders in a
Directory
43 COMMENTS
EXCEL
2 Excel VBA, Find and
List All Files in a
Directory and its
Subdirectories
33 COMMENTS
EXCEL
3 VBA Excel, Writing to a
Text File
26 COMMENTS
EXCEL
4 Excel VBA Open File
Dialog
25 COMMENTS
EXCEL
5 List All Files in a Folder
and Create Hyperlinks
to Each File, Excel VBA
23 COMMENTS
AUTOMATION
6 VBA Word, Split Word
File into Multiple Files
(Every X Pages)
18 COMMENTS
DATA VALIDATION
7 Excel VBA Drop Down
Lists Using Data
Validation
18 COMMENTS
EXCEL
8 Excel VBA, Save
Range/Cells as JPEG
17 COMMENTS
Privacy Policy