Access of Text in an Excel Autoshape with vba

%3CLINGO-SUB%20id%3D%22lingo-sub-2061075%22%20slang%3D%22en-US%22%3EAccess%20of%20Text%20in%20an%20Excel%20Autoshape%20with%20vba%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2061075%22%20slang%3D%22en-US%22%3E%3CP%3EHow%20can%20I%20access%20Text%20in%20an%20Excel%20Autoshape%20with%20vba%20(see%20worksheet%20in%20attached%20file)%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2061075%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECharting%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2061222%22%20slang%3D%22en-US%22%3ERe%3A%20Access%20of%20Text%20in%20an%20Excel%20Autoshape%20with%20vba%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2061222%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F712050%22%20target%3D%22_blank%22%3E%40ber999%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3E%20%20%20%20Dim%20strText%20As%20String%0A%20%20%20%20strText%20%3D%20ActiveSheet.Shapes(1).TextFrame2.TextRange.Text%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EIf%20you%20know%20the%20name%20of%20the%20shape%2C%20you%20can%20use%20that%3A%20change%20Shapes(1)%20to%20Shapes(%22Flussdiagramm%3A%20Prozess%201%22)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2063825%22%20slang%3D%22en-US%22%3ERe%3A%20Access%20of%20Text%20in%20an%20Excel%20Autoshape%20with%20vba%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2063825%22%20slang%3D%22en-US%22%3E%3CP%3EHallo%20Hans%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethank%20you%20very%20much.%20I%20have%20found%20this%20method%20at%20several%20places%20in%20the%20internet.%20But%2C%20why%20does%20not%20work%3F%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2064469%22%20slang%3D%22en-US%22%3ERe%3A%20Access%20of%20Text%20in%20an%20Excel%20Autoshape%20with%20vba%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2064469%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Hans%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethank%20you%20for%20the%20answer.%20It%20seems%20that%20some%20shapetypes%20have%20a%20different%20object%20model.%20I%20am%20looking%20for%20an%20object%20model%20for%20the%20Autoshapes.%26nbsp%3B%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2064404%22%20slang%3D%22en-US%22%3ERe%3A%20Access%20of%20Text%20in%20an%20Excel%20Autoshape%20with%20vba%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2064404%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F712050%22%20target%3D%22_blank%22%3E%40ber999%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDo%20you%20get%20an%20error%20message%3F%20If%20so%2C%20what%20does%20it%20say%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

How can I access Text in an Excel Autoshape with vba (see worksheet in attached file)?

15 Replies

@ber999 

For example:

    Dim strText As String
    strText = ActiveSheet.Shapes(1).TextFrame2.TextRange.Text

If you know the name of the shape, you can use that: change Shapes(1) to Shapes("Flussdiagramm: Prozess 1")

Hallo Hans,

 

thank you very much. I have found this method at several places in the internet. But, why does not work?

@Hans Vogelaar 

@ber999 

Do you get an error message? If so, what does it say?

Hi Hans,

 

thank you for the answer. It seems that some shapetypes have a different object model. I am looking for an object model for the Autoshapes.  @Hans Vogelaar 

@ber999 

The code works in the workbook that you attached. I'd have to see an example of where it fails.

Hi  Hans,

thank you very much. I have copied the Autoshape at the attached sheet from a worksheet with hundreds of different shapes. My vba code did not work there with the Autoshapes only.

I have found code for Powerpoint Autoshapes. The access method is different there (see below).

Dim shpTextArray() As Variant

Dim numShapes, numAutoShapes, i As Long



Set myDocument = ActivePresentation.Slides(1)

With myDocument.Shapes
    numShapes = .Count
    If numShapes > 1 Then
        numTextShapes = 0
        ReDim shpTextArray(1 To 2, 1 To numShapes)
        For i = 1 To numShapes
            If .Item(i).HasTextFrame Then
                numTextShapes = numTextShapes + 1
                shpTextArray(numTextShapes, 1) = .Item(i).Name
                shpTextArray(numTextShapes, 2) = .Item(i) _
                    .TextFrame.TextRange.Text
            End If
        Next
        ReDim Preserve shpTextArray(1 To 2, 1 To numTextShapes)
    End If
End With

 @Hans Vogelaar 

Hi Hans,

 

this is the interesting part of the code I have used:

.

Dim exlShpOuter As Excel.Shape

.

.

Set exlWssrc=ActiveWorkbook.Worksheets.Item("AS")

 

.

For Each exlShpOuter In exlWsSrc.Shapes

Select Case exlShpOuter.Type
   Case Office.msoLine
   Case Office.msoGroup
 .

 .

  Case Else
.

 

vbaText = exlShpOuter.TextFrame2.TextRange.Text

.

.

The error messagebox  is attached.

 

 

 

 

 

@Hans Vogelaar 

@ber999 

I don't see "the attached sheet"...

A screenshot won't help.

Hi, 

 

thank you for the notice. The error message is "Der
angegebene Wert ist außerhalb des zulässigen Bereichs."

@ber999 

@ber999 

I did see the screenshot (and I understand German), but that doesn't help. I'd need to see (a copy of) the workbook.

Hi Hans,

 

I have tested the my code with the worksheet and the single Autoshape. And, it works. Consequently, the problem should be the structure of the worksheet. However, the worksheet in question is not for public use. So, I will try to find a solution.

@Hans Vogelaar 

Hi Hans,

 

the attached file should reproduce the error.@ber999 

@ber999 

You can test whether a shape has text: change

        vbaText = exlShpOuter.TextFrame2.TextRange.Text

to

        If exlShpOuter.TextFrame2.HasText Then
            vbaText = exlShpOuter.TextFrame2.TextRange.Text
        End If

Hi Hans,

 

it works good. But, there must at least one shape without text exist which is not visible to me and not handled by the select cases instructions above else.
@Hans Vogelaar 

@ber999 

Does that happen in your sample workbook? If so, where should I look?