Resize Images of a Specific Style Only

T

Tom

I have a macro that resizes all images in a Word document to 75% of
their original size. I would like to make it so that the macro only
resizes images that have a certain style applied, such as
images_steps. How would I do that?

Here's the image macro for resizing.

Sub ImageShrink()
Dim PecentSize As Integer
Dim oIshp As InlineShape
Dim oshp As Shape

PercentSize = InputBox("Enter percent of full size", "Resize
Picture", 75)

For Each oIshp In ActiveDocument.InlineShapes
With oIshp
.ScaleHeight = PercentSize
.ScaleWidth = PercentSize
End With
Next oIshp

For Each oshp In ActiveDocument.Shapes
With oshp
.ScaleHeight Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
.ScaleWidth Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
End With
Next oshp
End Sub
 
L

Lene Fredborg

You can insert style checks around the code that iterates through the inline
shapes and shapes like this:


Inline shapes – check the style of the paragraph in which the inline shape
is found:

With oIshp
If oIshp.Range.Style = "images_steps" Then
.ScaleHeight = PercentSize
.ScaleWidth = PercentSize
End If
End With

Shapes – check the style of the paragraph to which the shape is anchored (if
that is actually what you want):

With oshp
If oshp.Anchor.Paragraphs(1).Range.Style = "images_steps" Then
.ScaleHeight Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
.ScaleWidth Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
End If
End With

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
M

macropod

Hi Tom,

Like this:
Sub ReformatPics()
Dim PercentSize As Integer
Dim MyStyle As String
Dim oIshp As InlineShape
Dim oshp As Shape
PercentSize = InputBox("Enter percent of full size", "ResizePicture ", 75)
MyStyle = "images_steps"
With ActiveDocument
For Each oIshp In .InlineShapes
With oIshp
If .Range.Paragraphs(1).Style = MyStyle Then
.ScaleHeight = PercentSize
.ScaleWidth = PercentSize
End If
End With
Next oIshp
For Each oshp In .Shapes
With oshp
If .Anchor.Paragraphs(1).Style = MyStyle Then
.ScaleHeight Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
.ScaleWidth Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
End If
End With
Next oshp
End With
End Sub

Note: You had a typo in 'Dim PercentSize'.
 
H

Helmut Weber

Hi Tom,

like that:

Sub Test10()
Dim oInl As InlineShape
Dim oShp As Shape
For Each oInl In ActiveDocument.InlineShapes
If oInl.Range.Style = "Normal" Then
MsgBox "Inlineshape: Normal"
End If
Next
For Each oShp In ActiveDocument.shapes
If oShp.Anchor.Style = "Body Text" Then
MsgBox "Shape: Body Text"
End If
Next
End Sub
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
T

Tom

Thanks so much for all your responses. They all probably do the same
thing, but I used the one Macropod wrote because it worked on the
first try for me.
 

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