insert picture macro

D

Dave

Hello All,
I'm using an insert/format picture macro that works without any problem
until I try and insert a picture into a table. The code below will return an
iIndex value of 1 or greater when used outside of a table. When I use it in a
table iIndex returns a value of 0 and errors out. Any ideas?

If .Name <> "" Then
iIndex = ActiveDocument.Shapes.Count
ActiveDocument.Shapes(iIndex).Select
 
C

Chuck

Not without more code. What you've included isn't enough to give a full
picture of what you're doing.
 
D

Dave

Hi Chuck,
Thanks for the reply, the entire code for the macro is below. I didn't write
the macro, but it's causing some problems with some existing templates,
inserting pictures into a table.

' Macro created 1/26/2004 by jct6316
Dim iIndex As Integer
'
With Dialogs(wdDialogInsertPicture)
.FloatOverText = True
.Show
If .Name <> "" Then
iIndex = ActiveDocument.Shapes.Count
ActiveDocument.Shapes(iIndex).Select
Selection.ShapeRange.Fill.Visible = msoFalse
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoFalse
Selection.ShapeRange.LockAspectRatio = msoTrue
Selection.ShapeRange.Height = 129.6
Selection.ShapeRange.Width = 172.8
Selection.ShapeRange.PictureFormat.Brightness = 0.5
Selection.ShapeRange.PictureFormat.Contrast = 0.5
Selection.ShapeRange.PictureFormat.ColorType = msoPictureAutomatic
Selection.ShapeRange.PictureFormat.CropLeft = 0#
Selection.ShapeRange.PictureFormat.CropRight = 0#
Selection.ShapeRange.PictureFormat.CropTop = 0#
Selection.ShapeRange.PictureFormat.CropBottom = 0#
Selection.ShapeRange.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionColumn
Selection.ShapeRange.RelativeVerticalPosition = _
wdRelativeVerticalPositionParagraph
Selection.ShapeRange.Left = InchesToPoints(0)
Selection.ShapeRange.Top = InchesToPoints(0)
Selection.ShapeRange.LockAnchor = False
Selection.ShapeRange.WrapFormat.AllowOverlap = True
Selection.ShapeRange.WrapFormat.Side = wdWrapBoth
Selection.ShapeRange.WrapFormat.DistanceTop = InchesToPoints(0)
Selection.ShapeRange.WrapFormat.DistanceBottom = InchesToPoints(0)
Selection.ShapeRange.WrapFormat.DistanceLeft = InchesToPoints(0.13)
Selection.ShapeRange.WrapFormat.DistanceRight = InchesToPoints(0.13)
Selection.ShapeRange.WrapFormat.Type = wdWrapTopBottom
ActiveWindow.ActivePane.SmallScroll Down:=-15
Selection.ShapeRange.Line.Weight = 1#
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.Style = msoLineSingle
End If
End With
End Sub
 
C

Chuck

Hi Dave

When the code inserts a picture into a table the picture is inserted as an
InlineShape, not a Shape, so Activedocument.Shapes.Count doesn't include it
in the count (Activedocument.InlineShapes.Count does). You could convert the
InlineShape to a Shape before adjusting its settings but I'm not sure what
the point of inserting an image into a table is if it doesn't stay in the
cell its inserted into, in which case I would think an InlineShape would be
appropriate.

InlineShapes and Shapes have different properties so while the code works ok
with shapes it doesn't with InlineShapes.

The code sets LockAspectRatio to True, but then sets both height and width,
regardless of whether those measurements maintain the aspect ratio for a
particular image. This works with Shapes in my testing, but with
InlineShapes setting both height and width negates the LockAspectRatio
setting, resulting in a distorted image.

I've rewritten the code as below to determine whether the picture is being
inserted in a table or not, then sets an object (objPic) and its appropriate
settings accordingly. I'm not sure what the SmallScroll line was supposed to
do (possibly a relic from macro recording) so I've deleted that line. I also
commented out the Height setting because it wasn't necessary for Shapes
(aspect ratio being locked) and it distorted InlineShapes in cells in my
testing. You can change which line to disable (Height or Width) as
appropriate, for instance depending on whether you want the image to fit the
height or width of a table cell.

There may be other ways of handling this situation but I tried to stick as
closely as possible to what you've got.


Sub InsertPic()

Dim iIndex As Integer
Dim ojbPic As Object

With Dialogs(wdDialogInsertPicture)
.FloatOverText = True
.Show
If .Name <> "" Then
If Selection.Information(wdWithInTable) = True Then
iIndex = ActiveDocument.InlineShapes.Count
Set objPic = ActiveDocument.InlineShapes(iIndex)
Else
iIndex = ActiveDocument.Shapes.Count
Set objPic = ActiveDocument.Shapes(iIndex)
'these properties are only applicable to Shapes
With objPic
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = _
wdRelativeVerticalPositionParagraph
.Left = InchesToPoints(0)
.Top = InchesToPoints(0)
.LockAnchor = False
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapBoth
.WrapFormat.DistanceTop = InchesToPoints(0)
.WrapFormat.DistanceBottom = InchesToPoints(0)
.WrapFormat.DistanceLeft = InchesToPoints(0.13)
.WrapFormat.DistanceRight = InchesToPoints(0.13)
.WrapFormat.Type = wdWrapTopBottom
End With
End If
'these properties apply to both Shapes and InlineShapes
With objPic
.Fill.Visible = False
.Fill.Visible = msoFalse
.Fill.Transparency = 0#
.Line.Weight = 0.75
.Line.DashStyle = msoLineSolid
.Line.Style = msoLineSingle
.Line.Transparency = 0#
.Line.Visible = msoFalse
.LockAspectRatio = msoTrue
' .Height = 129.6
.Width = 172.8
.PictureFormat.Brightness = 0.5
.PictureFormat.Contrast = 0.5
.PictureFormat.ColorType = msoPictureAutomatic
.PictureFormat.CropLeft = 0#
.PictureFormat.CropRight = 0#
.PictureFormat.CropTop = 0#
.PictureFormat.CropBottom = 0#
.Line.Weight = 1#
.Line.Visible = msoTrue
End With
End If
End With
End Sub
 

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