Word VBA Resize Pictures - VBA and VB.net Tutorial
Word VBA Resize Pictures - VBA and VB.net Tutorial
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
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:
InlineShapes.Item(1).ScaleHeig
ht = 90
Example:
Shapes.Item(1).ScaleWidth = 40
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:
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
lngScaledHeight =
InlineShapes.Item(1).Height
InlineShapes.Item(1).ScaleHeig
ht = 100
'the size of the original
image
lngOriginalHeight =
InlineShapes.Item(1).Height
InlineShapes.Item(1).ScaleHeig
ht = _
lngScaledHeight /
lngOriginalHeight * 100
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:
'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
Set objDocument =
Documents.Add
InlineShapes.Item(1).Select
Selection.Copy
objDocument.Activate
Selection.Paste
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
Resize Pictures.docm
See also:
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
[?]
Hair Restoration in Banda Aceh
Might Be Way Cheaper Than You…
Hair Transplant Clinic | Search Ads
RECENT POSTS
IF
CC SS
ACCESS
EXCEL
OPERATORS
EXCEL
POPULAR POSTS
FILE AND FOLDERS
43 COMMENTS
EXCEL
33 COMMENTS
EXCEL
26 COMMENTS
EXCEL
EXCEL
23 COMMENTS
AUTOMATION
18 COMMENTS
DATA VALIDATION
18 COMMENTS
EXCEL
17 COMMENTS
Privacy Policy