none
Reading Shape Text RRS feed

  • Question

  • I have to programmatically read a word document which was converted from pdf by some software. Throughout the document, there are text embedded in Shapes objects. I read the document 1 paragraph at a time but the text contained in textbox shapes are not read. Is there a way to know what Shape text is contained within a paragraph?

    Thanks


    50% of programming is coding. The other 90% is debugging

    Wednesday, March 23, 2016 6:54 PM

Answers

  • >>>Is there a way to know what Shape text is contained within a paragraph?

    According to your description, you could use Use the TextFrame property to return the TextFrame object for a shape. The TextRange property returns a Range object that represents the range of text inside the specified text frame, you could refer to below code:
    Sub Demo()
    
        Dim oPara As Paragraph
        Dim oShp As Shape
         
        For Each oPara In ActiveDocument.Paragraphs
            For Each oShp In oPara.Range.ShapeRange
                 'do whatever you want here
                Debug.Print oShp.TextFrame.TextRange.Text
            Next
        Next
          
    End Sub

    For more information, click here to refer about TextFrame Object (Word)

    • Marked as answer by JonWayn Friday, March 25, 2016 1:29 AM
    Thursday, March 24, 2016 1:52 AM

All replies

  • >>>Is there a way to know what Shape text is contained within a paragraph?

    According to your description, you could use Use the TextFrame property to return the TextFrame object for a shape. The TextRange property returns a Range object that represents the range of text inside the specified text frame, you could refer to below code:
    Sub Demo()
    
        Dim oPara As Paragraph
        Dim oShp As Shape
         
        For Each oPara In ActiveDocument.Paragraphs
            For Each oShp In oPara.Range.ShapeRange
                 'do whatever you want here
                Debug.Print oShp.TextFrame.TextRange.Text
            Next
        Next
          
    End Sub

    For more information, click here to refer about TextFrame Object (Word)

    • Marked as answer by JonWayn Friday, March 25, 2016 1:29 AM
    Thursday, March 24, 2016 1:52 AM
  • Thanks for the reply. I do find though that when I iterate all the shapes from a paragraph object, I instead get all the shapes contained on the entire page instead.

    Dim Parag As Pargraph, Shp as Shape

    If Parag.Range.ShapeRange.Count > 0 Then              'if parag has none, Out of Memory error is raise
         For Each Shp In Parag.Range.ShapeRange
             If Shp.Type = msoTextBox Then
                Cnt = Cnt + 1
                Shp.Select
                Debug.Print Shp.TextFrame.TextRange.Text
             End If
         Next
     End If


    50% of programming is coding. The other 90% is debugging


    • Edited by JonWayn Thursday, March 24, 2016 3:03 AM
    Thursday, March 24, 2016 3:01 AM
  • So I found a workaround. It's inelegant, but it does the job. I like to share my findings just so someone else may benefit from it. After acquiring each paragraph in the document, I read its ShapeRange Count property to see if it has any Shape objects in it (since For Each Shp raises an error if there is no shape to the paragraph). What I have found is that all shapes contained on a page are contained in the last paragraph of the page only. I am assuming that they were anchored there (and might not always be the case in all documents), even though they are displayed throughout the entire page. Somehow, reading the ShapeRange Count property for the 1st page always return 0 even when there are shapes on that page. I haven't figured out why that is so. Luckily for me, the shapes are arranged in a fixed order so even though I am getting them at the end of each page, once I get to them, I have a standard way of identifying where each belongs

    50% of programming is coding. The other 90% is debugging

    Friday, March 25, 2016 1:29 AM
  • I there a way to display shape anchors similarly to how paragraph marks can be displayed in Word?

    50% of programming is coding. The other 90% is debugging

    Friday, March 25, 2016 1:36 AM
  • >>>I there a way to display shape anchors similarly to how paragraph marks can be displayed in Word?

    According to your description, as far as I know that Object anchor shows the location of a floating object which is in relation to the text in your document. Keep in mind that the anchors just display when it was wrapped around by text or you have the picture selected.

    So I suggest that you could submit any feedback to User Voice:

    https://word.uservoice.com/

    Thanks for your understanding.
    Tuesday, March 29, 2016 7:54 AM