Which word shape types have text frames

User avatar
StuartR
Administrator
Posts: 12789
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Which word shape types have text frames

Post by StuartR »

I have some Word VBA code that has always worked without error. It includes the following:

    For Each MyShape In MyHeader.Shapes
            If MyShape.TextFrame.HasText Then _
                MyShape.TextFrame.TextRange.Fields.Update
    Next MyShape

This code still works perfectly on Word 2003, but on Word 2007 it gives an error when the Shape is a picture, and I suspect it would fail for some other types of shape too.

I looked on MSDN and found a helpful note that says.
MSDN wrote: Some shapes do not support attached text (lines, freeforms, pictures, and OLE objects, for example). If you attempt to return or set properties that control text in a text frame for those objects, an error occurs.
I'm happy to add a Select Case MyShape.Type to select only shapes with TextFrames, but I can't find a list of which shape types have TextFrames and which ones don't.

Any suggestions?
StuartR


User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16172
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Which word shape types have text frames

Post by ChrisGreaves »

StuartR wrote:"If you attempt to return or set properties that control text in a text frame for those objects, an error occurs.
My general principle in areas where VBA seems to have left-something-out is to write a developer function that makes use on On Error to trap the unknown. Such a function might start testing for all known types, and at the first error, jump to an error label that sets the result as "unknown" or "unmanageable".

Air Code:

Code: Select all

Function blnShapeSupportsAttachedText(shp As Shape) As Boolean
    blnShapeSupportsAttachedText = True
    On Error GoTo NotSupported
        If shp.TextFrame.HasText Then
        Else
        End If
    Exit Function
NotSupported:
    blnShapeSupportsAttachedText = False
End Function
The brain is a three-pound mass you can hold in your hand that can conceive of a universe a hundred billion light-years across (Marian C. Diamond)

GeoffW
PlatinumLounger
Posts: 4147
Joined: 24 Jan 2010, 07:23

Re: Which word shape types have text frames

Post by GeoffW »

The principle is good- but goto is not encouraged- and indeed does not give a return point.

The on error processing should test for the type of error otherwise different errors may get trapped inadvertantly.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 16172
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Which word shape types have text frames

Post by ChrisGreaves »

GeoffW wrote:The principle is good- but goto is not encouraged- and indeed does not give a return point.
The on error processing should test for the type of error otherwise different errors may get trapped inadvertantly.
I agree, and I agree. I was waving my hands about "In principle".
As you recall (grin!) I dislike On Error, except for a method of overcoming design limitations in the translator.
The brain is a three-pound mass you can hold in your hand that can conceive of a universe a hundred billion light-years across (Marian C. Diamond)

User avatar
StuartR
Administrator
Posts: 12789
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Which word shape types have text frames

Post by StuartR »

Thanks to everyone. I have used a simplified version of Chris's code.

Code: Select all

Function blnShapeSupportsAttachedText(shpIn As Shape) As Boolean
    blnShapeSupportsAttachedText = False
    On Error GoTo Done
    If shpIn.TextFrame.HasText Then blnShapeSupportsAttachedText = True
Done:
End Function
This is good enough for me as I only use it to check that I won't get an error from the line

Code: Select all

If blnShapeSupportsAttachedText(MyShape) then MyShape.TextFrame.TextRange.Fields.Update
Since all I want to do is update all fields in all shapes, I don't care WHY that returns an error, or even what error it returns, I just want to update fields in shapes where I can get at them.
StuartR


User avatar
Jan Karel Pieterse
5StarLounger
Posts: 658
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: Which word shape types have text frames

Post by Jan Karel Pieterse »

I would have used a slightly different method:

Code: Select all

Function blnShapeSupportsAttachedText(shpIn As Shape) As Boolean
    On Error Resume Next
    blnShapeSupportsAttachedText = shpIn.TextFrame.HasText
End Function
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
StuartR
Administrator
Posts: 12789
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Which word shape types have text frames

Post by StuartR »

Jan Karel Pieterse wrote:I would have used a slightly different method:
Even shorter, and just as clear to read. Thank you.
StuartR