Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Setting Picture Layout Wrapping Style with Macro

6,398 views
Skip to first unread message

Michael Goerz

unread,
Nov 18, 2007, 2:58:30 PM11/18/07
to
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

Jay Freedman

unread,
Nov 18, 2007, 9:03:44 PM11/18/07
to
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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

Michael Goerz

unread,
Nov 19, 2007, 5:41:26 PM11/19/07
to
Jay Freedman wrote:
> 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

Jay Freedman

unread,
Nov 19, 2007, 7:32:49 PM11/19/07
to
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.

--

Michael Goerz

unread,
Nov 20, 2007, 8:47:17 AM11/20/07
to
Jay Freedman wrote:
> 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

gzep

unread,
Dec 6, 2007, 12:33:01 AM12/6/07
to
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?

Jay Freedman

unread,
Dec 6, 2007, 9:58:26 PM12/6/07
to
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.

passion....@gmail.com

unread,
Dec 11, 2013, 12:17:34 PM12/11/13
to
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.

d.gra...@yahoo.com

unread,
May 12, 2018, 9:03:06 PM5/12/18
to
Hi Jay I also would like to apply a set of setting to my picture and would need a macro written in VBA. Could you help me with that?

Doing it manually in Word 2016, I would choose the following settings in the Advanced Layout dialog:


Format Autoshape/picture>Layout
Layout
Wrapping style: tight
Horizontal alignment: other

Format Autoshape/picture>Layout>Advanced
Horizontal: Absolute position 2.54 cm to the right of Page
Vertical: Absolute position 2.54 cm below Page
Options:
Move object with text [not checked]
Allow overlap [not checked]
Layout in table cell [not checked]
Lock anchor [checked]

Then I can move it where I want it on the page and it stays.
I have hundreds of pictures to place so would save me alot of time.

Thanks David

dduf...@gmail.com

unread,
May 13, 2018, 1:44:43 PM5/13/18
to
David,
I have working code that needs two custom styles I created with the help of others here that may be close to what you need. The images end up "fitting" into user variable table cell sizes. Not perfect but with a little training a user can choose variables that more closely match the image format such as between a phone and regular camera.
My result is that anywhere in a Word document the user can select and embed "tables" of pictures formatted by user variable choices that spread between margins. Below each picture cell is another smaller cell that automatically gets populated with file name, a UniqueID, and the ability to add and flow as much descriptive text as the user needs for each image.
You can create a distributable template with this ability or embed it into your Normal.docx.
Multiple tables can be embededed into the same document and the UniqueID continues to flow.
My client uses this ability to embed and describe work his company is bidding on. They typically have about 80 images in a bid document.
Dennis
0 new messages