
Inline Shapes vs Fields - brain teaser
Hi everyone,
I've written the following piece of code to run through all inline shapes in
a document and unlink any Visio embedded OLE object. Interestingly, the
first msgbox says there are X many inlineshapes, and the second msgbox says
you've looped 2*X times. The problem appears to be that when you unlink an
inline shape of type wdInlineShapeEmbeddedOLEObject, if it's a graphic, it
becomes a wdInlineShapePicture. However, the Next statement in the loop
doesn't appear to make you move to the next shape. Instead it makes you
loop again with the same shape, only this time it's of the new
wdInlineShapePicture type and nothing happens. To confirm that Word was
indeed accessing the same shape a second time, I added a oShape.Select and
checked that the same shape was highlighted in the document. Only on the
third iteration do you actually access the second shape of the collection.
If however I use the alternate approach of looping through all Fields in the
document (see code below), this behaviour doesn't occur - I've stepped
through the code to verify this. I was hoping that looping through the
Inline Shapes collection would be faster than looping through all of the
fields in a document. Any ideas as to why this is happening?
Thanks
Sebastien
** Inline Shape approach
Public Sub ConvertVisioShapes()
Dim oShape As InlineShape
Dim i As Integer
MsgBox ActiveDocument.InlineShapes.Count
For Each oShape In ActiveDocument.InlineShapes
i = i + 1
'oShape.Select
If oShape.Type = wdInlineShapeEmbeddedOLEObject Then
If (InStr(1, oShape.OLEFormat.ClassType, "Visio", vbTextCompare)
Quote:
> 0) Then
oShape.Field.Unlink
End If
End If
Next
MsgBox "# of loops: " & i
End Sub
** Fields approach
Public Sub ConvertVisioShapes2()
Dim oField As Field
For Each oField In ActiveDocument.Fields
If oField.Type = wdFieldEmbed Then
If (InStr(1, oField.OLEFormat.ClassType, "Visio", vbTextCompare)
Quote:
> 0) Then
oField.Unlink
End If
End If
Next
End Sub