Setting Picture Layout Wrapping Style with Macro

M

Michael Goerz

Hi,

I have a document with with a number of pictures, for each of which I
want to set the Layout options to have them floating properly. Doing
this by hand requires to go to the Image Properties, to the Layout Tab,
go to Advanced, and set some options both in the Picture Position and
Text Wrapping tab. So, it's quite a few clicks, and I was hoping I could
put it all together as a macro, so that I just have to select a picture
an hit a shortcut to set all the options as I want them.

Unfortunately, it doesn't seem I can put the actions in a macro. When
the macro recorder is started, I can't do a right click on the image,
and even if I go through the 'Format' menu and the to 'Picture', all the
relevant options are inaccessible (grayed out). Is there a way to put
the Wrapping Style options in a Macro?

Thanks,
Michael Goerz
 
J

Jay Freedman

Hi Michael,

You can write such a macro in the VBA editor, you just can't do it
through the recorder.

If you describe the exact settings you want to use, we can tell you
what code to write.
 
M

Michael Goerz

Jay said:
Hi Michael,

You can write such a macro in the VBA editor, you just can't do it
through the recorder.

If you describe the exact settings you want to use, we can tell you
what code to write.
Doing it manually in Word 2003, I would choose the following settings in
the Advanced Layout dialog (at Format>Picture>Layout>Advanced):

In the 'Text Wrapping' tab:
Wrapping Style: Top and bottom
Distance from text: Top 0.2"
Bottom 0.2"

In the 'Picture Position' tab:
Horizontal Alignment: Centered relative to Page

All other values should stay whatever they are.

Thanks,
Michael
 
J

Jay Freedman

OK, here you go...

Sub FormatMyPicture()
Dim myShape As Shape

If Selection.InlineShapes.Count > 0 Then
Set myShape = Selection.InlineShapes(1).ConvertToShape
ElseIf Selection.ShapeRange.Count > 0 Then
Set myShape = Selection.ShapeRange(1)
Else
MsgBox "Please select a picture first."
Exit Sub
End If

With myShape
.WrapFormat.Type = wdWrapTopBottom
.WrapFormat.DistanceTop = InchesToPoints(0.2)
.WrapFormat.DistanceBottom = InchesToPoints(0.2)
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.Left = wdShapeCenter
End With
End Sub

The first group of statements makes sure there is a picture (or an
AutoShape or WordArt) selected, and if it's in line with text it
converts it to a floating shape. The second group of statements
applies the formatting you specified.
 
M

Michael Goerz

Jay said:
OK, here you go...

Sub FormatMyPicture()
Dim myShape As Shape

If Selection.InlineShapes.Count > 0 Then
Set myShape = Selection.InlineShapes(1).ConvertToShape
ElseIf Selection.ShapeRange.Count > 0 Then
Set myShape = Selection.ShapeRange(1)
Else
MsgBox "Please select a picture first."
Exit Sub
End If

With myShape
.WrapFormat.Type = wdWrapTopBottom
.WrapFormat.DistanceTop = InchesToPoints(0.2)
.WrapFormat.DistanceBottom = InchesToPoints(0.2)
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.Left = wdShapeCenter
End With
End Sub

The first group of statements makes sure there is a picture (or an
AutoShape or WordArt) selected, and if it's in line with text it
converts it to a floating shape. The second group of statements
applies the formatting you specified.
Cool!
Thanks so much!

Michale
 
G

gzep

Hi Jay,

I had the same question, so you answer has helped me too!

I find that the .ConvertToShape makes the object no longer an inline object,
and thus does not stay anchored to the position in the text where it was
pasted.

If I use the .ConvertToInlineShape, the formatting that I just applied gets
removed. Then I'm back to square 1.

if I convertToInlineShape, then format I get: "Object has been deleted"!

Any thoughts on how to make it inline again, and retain the formatting?
 
J

Jay Freedman

First, recognize that there are properties of Shape objects that
simply don't make sense for InlineShape objects, and vice versa. In
particular, all of the properties listed in the With clause of the
macro below can be applied to a Shape object, but are not defined for
an InlineShape object, which simply has no place to store that
information because it doesn't need it.

An InlineShape object behaves like a single character, so its position
is determined by the characters before it in the text. It has no use
for a WrapFormat property, a Left or Top property, and so on. Those
things apply only to Shape objects, which need them to determine
position on the page.

When you call ConvertToInlineShape, VBA simply discards all of the
Shape properties that don't apply to an InlineShape. Similarly, when
you call ConvertToShape, VBA discards any InlineShape properties that
don't apply to a Shape. You can see which these are by comparing the
lists of properties in the Help topics for the two kinds of objects.

Second, with respect to "ConvertToShape makes the object no longer an
inline object": Exactly. That's the whole point of calling the method!
Be very careful about the concept of "anchored" for a Shape object,
though. The "anchor" is the top left corner of the paragraph that
contained the insertion point at the time the Shape was created,
unless (a) you use the optional Anchor parameter of the .Add method to
specify a different range or (b) you later drag the anchor to a
different paragraph. Then the position of the Shape with respect to
that anchor point is specified by the .Left and .Top properties, as
modified by the .RelativeHorizontalPosition and
..RelativeVerticalPosition properties. You may have to read the Help a
few times and experiment a bit, but you'll be rewarded by being able
to control the shape's position. Then you won't have to worry about
converting to an InlineShape and losing formatting that it can't have.
 
P

passion.luv.gupta

Hi Jay,

I want to select multiple objects at one time and then apply the formatting (wrapping) . Can you help me with that ? Instead of selecting one image at one time and running the macro?

Secondly, I am trying to write a macro wherein I Can directly import more than One objects into the Document file and I want them to be shifted anywhere in the screen.
 

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