PDA

View Full Version : Solved: Shapes Add Picture



JohnT
11-16-2006, 09:59 AM
Hi,
Working in Word 2003.
I have a table of 8 columns and 2 rows.
The attached macro is designed to place an image in the odd numbered cells (i.e. 1,3,5,7 etc.) (later code then scales the image to fit the cell).
When selecting a graphic file (jpeg, usualy) I give the user the oportunity to remove the existing graphic, if one is not required.

The problem is the first time the following code runs it does not place the graphic, the second time it runs it does.

The only thought I have is that the anchor property is incorrect, but not sure why.

This is my first post and I do hope I have given enough information.
Regards
John

'Move selection point to start of cell 1,1
ActiveDocument.Tables(1).Cell(1, 1).Select
Selection.Collapse wdCollapseStart
'Add the picture to all cells
For cellcount = 1 To 8
ActiveDocument.Shapes.AddPicture FileName:=PicName, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Left:=-90, _
Top:=165, _
Anchor:=Selection.Range

'If at end of table do not try to select next cell
If cellcount <> 8 Then
Selection.MoveRight unit:=wdCell, Count:=2
Selection.Collapse wdCollapseStart
End If

Next

fumei
11-16-2006, 06:31 PM
If you really need them as Shapes, you may try:Sub AsShapes()
Dim intRow As Integer
Dim intCol As Integer
Dim var, var2
intRow = 1
intCol = 1
For var = 1 To 2 ' for the two rows
For var2 = 1 To 4 ' for the four cell in row
ActiveDocument.Shapes.AddPicture _
FileName:="C:\Temp\4AMc.jpg", _
LinkToFile:=False, _
SaveWithDocument:=True, _
Anchor:=ActiveDocument.Tables(1).Cell(intRow, intCol).Range
intCol = intCol + 2
Next
intRow = intRow + 1
intCol = 1
Next
End SubThis uses Range, rather than Selection to place the graphic. Using Range is better, most of the time, than Selection. You can use the Range as anchor.

However, are you sure you want them as Shapes? There IS the anchor issue. The anchors - especially with so many - could be shifted very easily.

You may want to consider using InlineShapes instead.Sub AsInlineShapes()
Dim intRow As Integer
Dim intCol As Integer
Dim var, var2
intRow = 1
intCol = 1
For var = 1 To 2 ' for the two rows
For var2 = 1 To 4 ' for the four cell in row
ActiveDocument.Tables(1).Cell(intRow, intCol).Range. _
InlineShapes.AddPicture FileName:="C:\Temp\4AMc.jpg", _
LinkToFile:=False, SaveWithDocument:=True
intCol = intCol + 2
Next
intRow = intRow + 1
intCol = 1
Next
End Sub

JohnT
11-17-2006, 03:24 AM
Hi.
Thanks for the suggestions.
I have to use Shapes as there is a chance the text will over run the picture, in some cases.
I have modified my code to your suggestion but the behavior is the same.
I have also noticed that the Show/Hide formatting marks also has an effec, so I'm going to play with that.
The other strange effect is that the following code does count the shapes I just cant see them!!
ishapecount = ActiveDocument.Shapes.Count
MsgBox ishapecount
Thanks again and do let me know if you have any other ideas.
John

JohnT
11-17-2006, 05:29 AM
Hi.
I have now changed the way I remove the images (by making them invisible, not by deleting them) and it now seems to work.
I still have no idea why the delete version didn't work - !!!
Thanks to all John
:sparkle: