Inline Shapes vs Fields - brain teaser 
Author Message
 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



Wed, 03 Jul 2002 03:00:00 GMT  
 Inline Shapes vs Fields - brain teaser
HI Sebastien,

This is one of many instances where Word loses track of the collection
pointer while you're working on a member of the collection.

The only solution is to develop a workaround that suits the situation.

--
Bill Coan
Microsoft Word MVP
Neenah WI 54956 USA
http://www.wordmacros.com
. . .

Quote:
> 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)
> > 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)
> > 0) Then
>                 oField.Unlink
>             End If
>         End If
>     Next

> End Sub



Wed, 03 Jul 2002 03:00:00 GMT  
 Inline Shapes vs Fields - brain teaser
Hi Sebastien,

Quote:
> However, the Next statement in the loop
> doesn't appear to make you move to the next shape.

As Bill says, we've seen this when you do something to change what
you're checking with For Each...Next. For example, if you're deleting
things. As a group, we've come to the conclusion that, when you're
doing something actually change the members in a collection, it's
better to use a construction such as:

    NrThings = InlineShapes.count
    For Counter = 1 to NrThings
        'Do something
    Next Counter

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://go.compuserve.com/MSOfficeForum

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :-)



Wed, 03 Jul 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Date/time algorithm for holidays (brain teaser?)

2. a synchronization brain teaser...

3. This one is a brain-teaser (for me anyway)

4. Brain Teaser

5. newbie brain teaser

6. Date Brain teaser

7. Brain teaser I hope

8. sub total brain teaser

9. SQL Brain Teaser - SELECTing from self-linked tables...

10. Looking for an idea, try this brain teaser...

11. Convert Inline to Shape and Anchor Shape

12. grouping shapes and convert to inline shape

 

 
Powered by phpBB® Forum Software