PDA

View Full Version : how to use loop for this



newbee
07-28-2013, 10:48 PM
hey can any one guide me how to use loop for croping images instead of this code which works for exactly seven images..so that the macro crops every image it encounters through the document and exits once the document is over...



Sub CropImageMacro()
'
' CropImageMacro Macro
' This Macro Crops unwanted parts of an image
'

Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=20, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1


Application.Templates.LoadBuildingBlocks
Dim sngHeight, sngWidth, sngCropTop, sngCropBottom As Single
sngCropTop = 0.154
sngCropBottom = 0.05


With ActiveDocument.InlineShapes(1)

sngHeight = .Height
sngWidth = .Width
With .PictureFormat

.CropTop = sngHeight * sngCropTop

.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height

.Width = .Width
End With

sngCropTop = 0.014
sngCropBottom = 0.069


With ActiveDocument.InlineShapes(2)

sngHeight = .Height
sngWidth = .Width
With .PictureFormat

.CropTop = sngHeight * sngCropTop

.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height

.Width = .Width
End With

With ActiveDocument.InlineShapes(3)

sngHeight = .Height
sngWidth = .Width
With .PictureFormat

.CropTop = sngHeight * sngCropTop

.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height

.Width = .Width
End With

With ActiveDocument.InlineShapes(4)

sngHeight = .Height
sngWidth = .Width
With .PictureFormat

.CropTop = sngHeight * sngCropTop

.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height

.Width = .Width
End With

With ActiveDocument.InlineShapes(5)

sngHeight = .Height
sngWidth = .Width
With .PictureFormat

.CropTop = sngHeight * sngCropTop

.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height

.Width = .Width
End With

With ActiveDocument.InlineShapes(6)

sngHeight = .Height
sngWidth = .Width
With .PictureFormat

.CropTop = sngHeight * sngCropTop

.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height

.Width = .Width
End With

With ActiveDocument.InlineShapes(7)

sngHeight = .Height
sngWidth = .Width
With .PictureFormat

.CropTop = sngHeight * sngCropTop

.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height

.Width = .Width
End With
End Sub

newbee
07-28-2013, 11:22 PM
If you have a .MoveDown
statement , you can use that to determine if a move down was
possible.

Like:

a = Selection.MoveDown unit:=wdLine

If the selection could be moved down 1 line, a gets the value 1. If not
(that happens when you are at the end of the document) a gets the value
0

So, if a=0 leave the loop.


This way the loop could be made to exit when the document is over.

newbee
07-29-2013, 01:21 AM
suppose if i write it as below and initiate n as 1, How can i check if there is a next image present in the document...?Anyone has any suggestion..

With ActiveDocument.InlineShapes(n)


sngHeight = .Height
sngWidth = .Width
With .PictureFormat


.CropTop = sngHeight * sngCropTop


.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height


.Width = .Width
End With


sngCropTop = 0.014
sngCropBottom = 0.069

n=n+1

fumei
07-29-2013, 01:47 PM
It looks like:

The FIRST image is cropped using:
sngCropTop = 0.154
sngCropBottom = 0.05

ALL the other are cropped using:
sngCropTop = 0.014
sngCropBottom = 0.069

Is this correct

newbee
07-30-2013, 10:59 PM
It looks like: The FIRST image is cropped using: sngCropTop = 0.154 sngCropBottom = 0.05 ALL the other are cropped using: sngCropTop = 0.014 sngCropBottom = 0.069 Is this correct

Yes thats correct...But thats not an issue...I wish the macro to run for every image contained in the document...Can have a same cropping dimension for all images...as sngCropTop=.014 sngCropBottom=.069 for all images.

fumei
08-01-2013, 02:23 PM
Dim sngHeight As Single
Dim sngWidth As Single
Dim sngCropTop As Single
Dim sngCropBottom As Single
Dim var

sngCropTop = 0.154
sngCropBottom = 0.05


With ActiveDocument.InlineShapes(1)
sngHeight = .Height
sngWidth = .Width
With .PictureFormat
.CropTop = sngHeight * sngCropTop
.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height
.Width = .Width
End With

sngCropTop = 0.014
sngCropBottom = 0.069

For var = 2 To ActiveDocument.InlineShapes.Count
With ActiveDocument.InlineShapes(var)
sngHeight = .Height
sngWidth = .Width
With .PictureFormat
.CropTop = sngHeight * sngCropTop
.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height
.Width = .Width
End With
Next This actions ALL images, regardless of how many. The FIRST getting one set of parameters, the rest getting the SECOND set of parameters. It uses the COUNT of inlineshapes. Of course all of the inlineshapes must be editable images.

fumei
08-01-2013, 04:14 PM
Oh and BTW, if all the inlineshapes are to be actioned, you could also do:

Dim oInlineShape As InlineShape

For Each oInlineShapes
...do stuff

Voila, all inlineshapes are actioned