Update pictures with VBA

Update pictures with VBA

Using this code I modify/delete some pictures in my project.
About modified pictures, when I open it for the first time look good, but starting from the second time the pictures is exactly how was before the change in VBA.
I don't understand why.
There is some special reason?
Am I doing something wrong?
Thanks
Set zPRJ = MyWorkspace.ActiveDocument
For Index = zPRJ.DynPictures.Count - 1 To 0 Step -1
Set zPIC = zPRJ.DynPictures.Item(zPRJ.DynPictures.Item(Index).Name)
If Left(zPIC.Name, 7) = "Layout." Then
If Right(zPIC.Name, Len(zPIC.Name) - 7) <> "Top" Then
If Right(zPIC.Name, Len(zPIC.Name) - 7) <> "Main" Then
zPRJ.DynPictures.Delete (zPIC.Name)
AddLogInfo "Picture: " & zPIC.Name & " deleted"
Else
For Index2 = zPIC.Elements.Count - 1 To 0 Step -1
zPIC.Elements.Delete (zPIC.Elements.Item(Index2).Name)
Next Index2
Sleep (500)
zPIC.Save
AddLogInfo "Picture: " & zPIC.Name & " updated"
End If
Else
For Index2 = zPIC.Elements.Count - 1 To 0 Step -1
If zPIC.Elements.Item(Index2).Name = "LoopSelection" _
Or zPIC.Elements.Item(Index2).Name = "GroupSelection" _
Or zPIC.Elements.Item(Index2).Name = "DeviceSelection" Then
zPIC.Elements.Delete (zPIC.Elements.Item(Index2).Name)
End If
Next Index2
Sleep (500)
zPIC.Save
AddLogInfo "Picture: " & zPIC.Name & " updated"
End If
End If
Next Index

This is a migrated post! Originally posted on 15.07.2008 by user fabios. Please be aware that information can be outdated.