Jean-Guy
Tue Jun 29 23:09:07 CDT 2004
Bonjour,
Dans son message, < Marty > écrivait :
In this message, < Marty > wrote:
|| Howdy All,
||
|| There are many spots where my routines draw a graphic (such as an
oval or a rectangle) and
|| then sets up an object to track that graphic. For example, at some later
point the code might
|| need to change the forecolor of all shapes held in AShapesSubset
collection. A problem arises
|| if the user should delete or group one of the shapes represented in the
AShapesSubset
|| collection. The collection's pointer for the affected shape is no longer
valid, and any attempt
|| to change the forecolor produces an error.
||
|| Is there a "best practice" means of tracking shapes? I wish that
shapes had events
|| associated with them; e.g. "ShapeDeleted" and a "ShapeGrouped" events.
It appears that this is
|| not the case (at least in Word 2000). Alternatively, is there a way to
alter Word's Delete and
|| Group routines so that I can first inspect the ShapeRange affected and
post my own event? I
|| can, of course, write code that would search through all Shapes and all
GroupShapes to check on
|| a shape's status. But that seems like quite a lot of overhead to
encompass every single time I
|| need to refer to a shape.
||
I would use the Name property to assign my own names to the shapes.
Later, I would use an InStr function to check if the shapes with my names
are still present.
Finally, check for grouped shapes, you can also "interrogate" members of a
group without ungrouping it.
Paste that code in a blank document module and play around with it to see
what you can do. Maybe you will find some useful stuff in there!
'_______________________________________
Const ShapeName As String = "MyShapes"
'_______________________________________
Sub AddShapes()
Dim MyShape As Shape
Set MyShape = ActiveDocument.Shapes _
.AddShape(msoShapeRectangle, _
113.8, 91.75, 149.8, 84.2)
MyShape.Name = ShapeName & "Rect1"
Set MyShape = ActiveDocument.Shapes _
.AddShape(msoShapeOval, _
340.25, 158.5, 102.8, 110.35)
MyShape.Name = ShapeName & "Ov1"
Set MyShape = ActiveDocument.Shapes _
.AddShape(msoShapeRectangle, _
127.15, 235.15, 36.6, 46.45)
MyShape.Name = ShapeName & "Rect2"
Set MyShape = Nothing
End Sub
'_______________________________________
'_______________________________________
Sub DoStuffWithShapes()
Dim MyShape As Shape
Dim SubMyShape As Shape
Dim i As Long
Const RCol As Long = 255
Const GCol As Long = 0
Const BCol As Long = 0
Dim FoundOne As Boolean
FoundOne = False
For Each MyShape In ActiveDocument.Shapes
With MyShape
If .Type = msoGroup Then
For i = 1 To .GroupItems.Count
Set SubMyShape = .GroupItems(i)
With SubMyShape
If InStr(.Name, ShapeName) > 0 Then
.Fill.ForeColor.RGB = RGB(RCol, GCol, BCol)
FoundOne = True
End If
End With
Next i
End If
If InStr(.Name, ShapeName) > 0 Then
.Fill.ForeColor.RGB = RGB(RCol, GCol, BCol)
FoundOne = True
End If
End With
Next MyShape
If Not FoundOne Then
MsgBox "No valid shapes were found", vbInformation, "No Shapes"
End If
Set MyShape = Nothing
Set SubMyShape = Nothing
End Sub
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site:
http://www.word.mvps.org