How to avoid selecting an object?

K

KB01

Below are two macros that were taken from an Excel VBA. They are
condensed to the minimum that still reproduces the problem. I need to
avoid selecting the object, but still need to read the text.
Can someone tell me why one macro works and the other does not?
Getting the AutoShapeType property (apparenty?) works. Why not getting
the text?
Is there a way to get the text from the rectangle object without
selecting it?

Thanks for any hints

KB

- - - - - - - - - -

Display text from all rectangles on a worksheet:

'This works, but selects the object which causes problems elsewhere,
e.g. if the worksheet is protected

Sub ShowText()
Dim OTbox As Object
For Each OTbox In ActiveSheet.Shapes
If OTbox.AutoShapeType = msoShapeRectangle Then
OTbox.Select
MsgBox Selection.Text
End If
Next OTbox
End Sub

'This returns an error 438 "Object does not support ..."

Sub ShowText()
Dim OTbox As Object
For Each OTbox In ActiveSheet.Shapes
If OTbox.AutoShapeType = msoShapeRectangle Then
MsgBox OTbox.Text
End If
Next OTbox
End Sub
 
R

Rick Rothstein

Does this macro do what you want?

Sub ShowText()
Dim R As Range
Dim OTbox As Object
Set R = Selection
For Each OTbox In ActiveSheet.Shapes
If OTbox.AutoShapeType = msoShapeRectangle Then
OTbox.Select
MsgBox Selection.Text
End If
Next OTbox
R.Select
End Sub
 
M

Mike H

Hi,

Try it like this

Sub ShowText()
Dim OTbox As Object
For Each OTbox In ActiveSheet.Shapes
If OTbox.AutoShapeType = msoShapeRectangle Then
MsgBox Shapes(OTbox.Name).TextFrame.Characters.Text
End If
Next OTbox
End Sub

Mike
 
R

Rick Rothstein

Actually, use this macro instead (it is your second macro modified to
work)...

Sub ShowText()
Dim OTbox As Object
For Each OTbox In ActiveSheet.Shapes
If OTbox.AutoShapeType = msoShapeRectangle Then
MsgBox OTbox.TextFrame.Characters.Text
End If
Next OTbox
End Sub
 

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