Image Handling with Macros

D

dpdalton

I tried to record a macro that would do two things to an image where the
cursor is located: resize it to a standard width (allowing the ratio to
control the height) and change its layout to Square, Right.

All went fine through the sizing, but when I clicked on the 'Layout' tab,
all the options were "greyed-out" so that I could not specify the layout I
wanted.

1. Why does this happen?

2. What can I do to accomplish my goal?

Here's what was recorded to the point where I could not select "Square".

'
Selection.InlineShapes(1).Fill.Visible = msoFalse
Selection.InlineShapes(1).Fill.Solid
Selection.InlineShapes(1).Fill.Transparency = 0#
Selection.InlineShapes(1).Line.Weight = 0.75
Selection.InlineShapes(1).Line.Transparency = 0#
Selection.InlineShapes(1).Line.Visible = msoFalse
Selection.InlineShapes(1).LockAspectRatio = msoTrue
Selection.InlineShapes(1).Height = 69.1
Selection.InlineShapes(1).Width = 69.1
Selection.InlineShapes(1).PictureFormat.Brightness = 0.5
Selection.InlineShapes(1).PictureFormat.Contrast = 0.5
Selection.InlineShapes(1).PictureFormat.ColorType = msoPictureAutomatic
Selection.InlineShapes(1).PictureFormat.CropLeft = 0#
Selection.InlineShapes(1).PictureFormat.CropRight = 0#
Selection.InlineShapes(1).PictureFormat.CropTop = 0#
Selection.InlineShapes(1).PictureFormat.CropBottom = 0#
End Sub

Thanks,
Paul
 
J

Jean-Guy Marcil

dpdalton was telling us:
dpdalton nous racontait que :
I tried to record a macro that would do two things to an image where
the cursor is located: resize it to a standard width (allowing the
ratio to control the height) and change its layout to Square, Right.

All went fine through the sizing, but when I clicked on the 'Layout'
tab, all the options were "greyed-out" so that I could not specify
the layout I wanted.

1. Why does this happen?

Inline shape cannot have a Wrap text attribute.
When using code, you have to explicitly convert the inline shape to a
floating one.
2. What can I do to accomplish my goal?

Here's what was recorded to the point where I could not select
"Square".

Try this:

Dim shpConvert As Shape

With Selection.InlineShapes(1)
.LockAspectRatio = msoTrue
.Height = 69.1
.Width = 69.1
Set shpConvert = .ConvertToShape
End With

With shpConvert.WrapFormat
.Type = wdWrapSquare
.Side = wdWrapRight
End With
 
D

dpdalton

Thanks, but that still doesn't seem to quite get it as I had hoped.

That macro code converts the image from InlineShape to a floating shape and
it applies the Square property to the image, but the image always ends up on
the left margin (with text wrapped to its right), rather than at the right
margin (with the text wrapped to the left of the image), as desired.

If it makes any difference, I'm doing this to images that are located within
text columns. However, I also tried this code on an image that was not in a
column and got the same result.

Any additional suggestions?

Paul
 
D

Doug Robbins - Word MVP

Replace

.Side = wdWrapRight

with

.Side = wdWrapLeft


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

dpdalton

Doug -

Thanks, but it really didn't change anything. After I substituted ".Side =
wdWrapLeft" as you suggested, the image still gets converted from an
InlineShape to a Shape, and it does set the format to WrapSquare, but when I
check "Format Picture", then "Layout", "Horizontal Alignment" shows as
"Other", not "Right" or "Left". Also, and hoping that it may help diagnose
the issue, starting from the "Layout" tab, I clicked on the "Advanced"
button, and found the following:

1. Under the "Picture Position" tab, "Horizontal" shows Absolute position
0.88" to the right of Page, "Vertical" shows Absolute position 3.34" below
Page, and "Options" has a checkmark next to "Layout in table cell".

2. Under the "Text Wrapping" tab, it shows "Wrapping style" as "Square",
"Wrap Text" shows "Left only", and "Distance from text" is 0" in all 4 boxes.


Hmmm. Just to see what would happen, I manually reformatted a different
image to look like it should and then I checked its settings. They are
different in several respects form those of the image that was formatted by
that macro:

1. On the "Layout" tab, it shows "Horizontal Alignment" as "Right."

2. Under the "Picture Position" tab, "Horizontal" shows Alignment Right
relative to Column, "Vertical" shows Absolute position 0.95" below Line, amd
"Options" has a checkmark next to "Layout in table cell" as well as next to
"Move object with text".

3. Under the "Text Wrapping" tab, it shows "Wrapping style" as "Square",
"Wrap Text" shows "Left only", and "Distance from text" is 0" in all 4 boxes.
(The settings under this tab appear to be the same on both images)

Those differences lead me to suspect that perhaps there are some other
settings I need to specify somehow in the macro to make the image relocate
itself to where I want it to be. Can you tell me what those would be? Of
course, I hope it can be done without me having to specify an "Absolute
position" for the Vertical as to each image, as I want to be able to run this
macro on many images that appear throughout several pages of this document,
which itself is in two text columns spanning many pages.

Thanks,
Paul
 
D

Doug Robbins - Word MVP

You probably need to use

With shpConvert
.Left = ActiveDocument.PageSetup.PageWidth - shpConvert.Width -
ActiveDocument.PageSetup.RightMargin
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
With .WrapFormat
.Type = wdWrapSquare
.Side = wdWrapRight
End With
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

dpdalton

Doug -

Thanks again. Here's the macro as it appears now.

Sub Tmp()
Dim shpConvert As Shape

With Selection.InlineShapes(1)
..LockAspectRatio = msoTrue
..Height = 69.1
..Width = 69.1
Set shpConvert = .ConvertToShape
End With

With shpConvert
..Left = ActiveDocument.PageSetup.PageWidth - shpConvert.Width -
ActiveDocument.PageSetup.RightMargin
..RelativeHorizontalPosition = wdRelativeHorizontalPositionPage

With .WrapFormat
..Type = wdWrapSquare
..Side = wdWrapRight
End With
End With
End Sub

It would not run as copied from your message until I placed the line that
starts with "ActiveDocument" at the end of the preceding line (maybe what you
typed wordwrapped?)

Your suggested addition does ALMOST everything I wanted. The one remaining
issue is this:

Within the two text columns on each page: When the image is located in the
right column, the macro puts the image exactly where I would like for it to
be.

However, when the image is in the LEFT text column, the macro now moves the
image over to the RIGHT column and then places it where it should go if it
had originally started within that right column.

What can I add to make it keep images in the left column WITHIN that left
column?

- Paul
 
D

Doug Robbins - Word MVP

In that case, you should use:

With shpConvert
.Left = ActiveDocument.PageSetup.TextColumns.Width - shpConvert.Width
.RelativeHorizontalPosition=wdRelativeHorizontalPositionColumn
With .WrapFormat
.Type = wdWrapSquare
.Side = wdWrapRight
End With
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

dpdalton

Doug -

It still didn't quite work right, but I kept playing with it and finally
figured out that there was one important missing component of the macro. I
wanted to report that here, in case anyone else goes down this path. I think
it now works as I wanted, though I'm going to be watching closely as I use it
to see if anything else is getting messed up that I've missed <g>.

(Because this thread has gotten so long, I've dropped the additional copies
of all our prior messages from this one, as they all appear above.)

Here's how I found what seems to work --

I made two changes to the text you gave me last time -- (1) to cause the
text to wrap on the left side of the image (which I had wanted), I changed
"Side = wd.WrapRight" to "Side = wd.WrapLeft". That, however, still left
the image itself positioned left of the left margin of the column, and I had
wanted it to be on the right side of whichever column it originally was
located wrapped on the left by adjacent text (that previously had been above
or below it).

I couldn't tell anything myself from just looking at the macro code, so I
thought maybe I could record a macro doing just the piece that I wanted and
then look at the macro code that Word creates to see if I could figure out
what was going on.

As you remember, I learned early in this thread that my initial problem with
trying to "macro record" all of this was because the images I've been using
all start out as inline imagse (as opposed to shapes with wrapped text). So,
I took another copy of one of the original images, positioned it in the
middle of the left text column and manually changed the image's format
setting to to Square, and then I deliberately set it to Left because I knew I
wanted the image to end up on the Right, so starting from "Left" would give a
recorded macro something to change.

Then, I recorded a macro to change the image location to the Right and to
set the Text to wrap only on the left side of the image.

Next, I looked at the recorded macro. Of course, by recording, I got a LOT
of code that I didn't need. BUT, I did find a nugget: One line was new and
seemed related, so I clipped it and pasted it into my macro, ran the macro,
and it seems to have done the trick. That line is:

..Left = wdShapeRight

Apparently, that's what causes the image to jump to the right side of the
column.

So, now, here is the macro that I run after selecting an image within a text
column when I want it to be resized smaller, positioned on the right side of
the same text column where it started, and have adjacent text wrapped to the
left side of that image:

Sub Tmp()
Dim shpConvert As Shape
With Selection.InlineShapes(1)
.LockAspectRatio = msoTrue
.Height = 69.1
.Width = 69.1
Set shpConvert = .ConvertToShape
End With
With shpConvert
.Left = ActiveDocument.PageSetup.TextColumns.Width - shpConvert.Width
.Left = wdShapeRight
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
With .WrapFormat
.Type = wdWrapSquare
.Side = wdWrapLeft
End With
End With
End Sub

Thanks for all your help!
Paul
 

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