Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do I Goto Shapes by "name"

Status
Not open for further replies.

RH610

Programmer
Aug 7, 2001
152
US
In Word2000 I have a macro that creates a document. After setting up the text I want the macro to insert shapes (pictures) at various places (which works). I want to name each shape (picture) as I insert them. I then want to goto each one by name and set the wrap characteristics, etc. I have enclosed my code which does not seem to work. I'm not sure if it's the naming command that is wrong or the goto command - or - both.

Thank You


'Start Insert Picture Section

If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdPrintView
Else
ActiveWindow.View.Type = wdPrintView
End If

Selection.Find.ClearFormatting
With Selection.Find.Font
.Size = 14
.Bold = False
.Italic = False
End With

For I = 1 To hms
With Selection.Find
.Text = Test$(I)
.Replacement.Text = Test$(I)
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdLine, Count:=6
Selection.HomeKey Unit:=wdLine
Dim char As Long
char = Selection.EndOf(Unit:=wdParagraph, Extend:=wdMove)
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Dim MyRange As Range
Set MyRange = ActiveDocument.Bookmarks("\Para").Range
ActiveDocument.Shapes.AddPicture FileName:="c:\test\" + Test$(I) + ".jpg" _
, LinkToFile:=False, SaveWithDocument:=True, Left:=36, Top:=200, Width:=288, Height:=169.2, Anchor:=MyRange
ActiveDocument.Shapes(1).Name = Test$(I)
Next

ActiveDocument.GoTo What:=wdGoToObject, Which:=wdGoToFirst, Name:="Shape"
With Selection.ShapeRange
.WrapFormat.Side = wdWrapLeft
.WrapFormat.DistanceTop = InchesToPoints(0)
.WrapFormat.DistanceBottom = InchesToPoints(0)
.WrapFormat.DistanceLeft = InchesToPoints(0.13)
.WrapFormat.DistanceRight = InchesToPoints(0.13)
.WrapFormat.Type = wdWrapTight
End With

For I = 1 To hms - 1
Selection.GoTo What:=wdGoToObject, Which:=wdGoToNext, Name:="Picture"
With Selection.ShapeRange
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapLeft
.WrapFormat.DistanceTop = InchesToPoints(0)
.WrapFormat.DistanceBottom = InchesToPoints(0)
.WrapFormat.DistanceLeft = InchesToPoints(0.13)
.WrapFormat.DistanceRight = InchesToPoints(0.13)
.WrapFormat.Type = wdWrapTight
End With
Next

'End Insert Pictures Section

 
RH610,

The following test procedure worked for me:

Code:
Sub InsertShape()
Dim Shp As Shape

  Set Shp = ActiveDocument.Shapes.AddPicture(FileName:="F:\Graphics\image01.jpg", LinkToFile:=False, SaveWithDocument:=True)
  With Shp
    .Name = "Pic1"
    .WrapFormat.Side = wdWrapLeft
    .WrapFormat.DistanceTop = InchesToPoints(0)
    .WrapFormat.DistanceBottom = InchesToPoints(0)
    .WrapFormat.DistanceLeft = InchesToPoints(0.13)
    .WrapFormat.DistanceRight = InchesToPoints(0.13)
    .WrapFormat.Type = wdWrapTight
  End With

End Sub

Setting a variable to the created shape allows you to easily manipulate its properties without selecting it. After naming it, you can then refer to a particular shape using the syntax
Code:
.Shapes("myShape")
elsewhere in your code.

HTH
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top