Why TextFrame Invalid Request Error for VBA code?

E

Evi

I have Powerpoint 97

I want my code to print all the text in text boxes in a file into the debug
window.

It works if the slide doesn't contain a .jpeg. If it does then I get the
error message

'TextFrame (Unknown member) invalid request. This type of shape cannot have
a TextRange'

But I thought that my code would test each shape and ignore those that don't
have text. What else can I do to eliminate these?

Evi
Here's the code:

Sub GetSlide()

'Copy the text from each file into the debug window

Dim SlideCount As Integer

Dim MyPres As Presentation

Dim Shp As Shape

Dim Sld As Slide

Dim TxtFrm As TextFrame

Dim AllSlides As Slides

Dim a As Integer

Dim b As Integer

Dim ShapeCount As Integer

Set MyPres = Application.ActivePresentation

For b = 1 To 30

'30 slides in presentation

Debug.Print b

Set Sld = MyPres.Slides(b)

ShapeCount = Sld.Shapes.Count

For a = 1 To ShapeCount

Set Shp = Sld.Shapes(a)

If Shp.HasTextFrame Then

If Shp.TextFrame.HasText Then

'why doesn't this bit reject the jpeg?

Debug.Print Shp.TextFrame.TextRange.Text

End If

End If

Next a

Next b

Set Sld = Nothing

Set Shp = Nothing

Set MyPres = Nothing

End Sub
 
S

Shyam Pillai

Evi,
You can't! It is a bug that is present. In earlier versions it was possible
to drag an image into a textbox/placeholder and that shape would become a
picture shape however when the shape text properties were queried it would
raise this error. This has never been fixed, the best practise is to include
an error handler specifically for this situation.
 
E

Evi

I have found one solution since I wrote to you. (For some reason solutions
often come after I've written for help!)

Picture files (unless renamed deliberately) seem to be called 'Picture'
something so I've put

'If InStr(1, ShpName, "Picture") = 0 Then'

ie if the name of the shape has the word 'Picture' in it.

So far, this seems to be working.

Here's my current code

Sub GetSlideText()

'Copy the text from each file into the debug window

'Evi Woolston

Dim SlideCount As Integer

Dim MyPres As Presentation

Dim Shp As Shape

Dim Sld As Slide

Dim TxtFrm As TextFrame

Dim AllSlides As Slides

Dim a As Integer

Dim b As Integer

Dim ShapeCount As Integer

Dim ShpName As String

Set MyPres = Application.ActivePresentation

SlideCount = MyPres.Slides.Count

For b = 1 To 5

'but you can't print more about 25 slides

'in the debug window or it cuts off the beginning

Debug.Print "Slide " & b

Set Sld = MyPres.Slides(b)

ShapeCount = Sld.Shapes.Count

For a = 1 To ShapeCount

Set Shp = Sld.Shapes(a)

ShpName = Shp.Name

If InStr(1, ShpName, "Picture") = 0 Then

'make sure that the shape isn't a picture

'if it is, then it will have Picture in its name

If Shp.HasTextFrame Then

If Shp.TextFrame.HasText Then

'make sure that the shape isn't a picture

Debug.Print Shp.TextFrame.TextRange.Text

End If

End If

End If

Next a

Next b

Set Sld = Nothing

Set Shp = Nothing

Set MyPres = Nothing

End Sub



Evi
 
S

Shyam Pillai

Evi,
Good, if it works for you. I wouldn't rely too much on this approach since
shape names can be changed.

I prefer to add a line - On Error Resume Next just prior to checking if
the shape has a text frame and then reset the error handler to it's previous
state after I close the If/End If structure.
 
S

Steve Rindsberg

See below, Evi

--
Posted to news://msnews.microsoft.com
Steve Rindsberg, PPT MVP
PowerPoint FAQ - www.pptfaq.com
PPTools - www.pptools.com
===============================

Evi said:
I have Powerpoint 97

I want my code to print all the text in text boxes in a file into the debug
window.

It works if the slide doesn't contain a .jpeg. If it does then I get the
error message

'TextFrame (Unknown member) invalid request. This type of shape cannot have
a TextRange'

But I thought that my code would test each shape and ignore those that don't
have text. What else can I do to eliminate these?

Evi
Here's the code:

Sub GetSlide()

'Copy the text from each file into the debug window

Dim SlideCount As Integer

Dim MyPres As Presentation

Dim Shp As Shape

Dim Sld As Slide

Dim TxtFrm As TextFrame

Dim AllSlides As Slides

Dim a As Integer

Dim b As Integer

Dim ShapeCount As Integer

Set MyPres = Application.ActivePresentation

For b = 1 To 30

Suggestion: change the above to:

For b = 1 to MyPres.Slides.Count

That way you don't have to change it for each presentation
'30 slides in presentation

Debug.Print b

Set Sld = MyPres.Slides(b)

ShapeCount = Sld.Shapes.Count

For a = 1 To ShapeCount

Set Shp = Sld.Shapes(a)

If Shp.HasTextFrame Then

If Shp.TextFrame.HasText Then

'why doesn't this bit reject the jpeg?

A guess: you're using XP or higher and haven't turned off
Tools, Autocorrect Options, Autoformat as you type, Automatic layout for
inserted objects

If this is left checked, your inserted JPGs may become Placeholder objects
instead of Embedded Picture objects. There are a few other oddball reasons
why this might happen as well.

Simplest thing might be to do:

If Shp.HasTextFrame Then
on error resume next
If Shp.TextFrame.HasText Then
etc.
End If
End if
' reset error trap
On Error GoTo 0 ' or on error goto YourErrorHandler
 
E

Evi

That's true, Shyam, the shapes could be renamed, then my trap won't work.
Shame I can't think of a better way of identifying the type of shape that it
is.

I did get some peculiar results with On Error Resume Next - I couldn't get
my head around resetting the error handler.
Evi
 
S

Shyam Pillai

In VBE if you look for help on the 'On error' statement, you will get the
relevant information.
 
E

Evi

It says, 'Disables any enabled error handler in the current procedure.'

That seems to imply that you have another error handler in the code.

Evi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top