RCarmody
RCarmody

Reputation: 720

VBA PowerPoint: Extract Shape Text from PowerPoint

I am trying to modify the existing PowerPoint VBA code below to include the Shape Text in addition to the other attributes listed. The purpose of this code is to pull every shape/text box and its attributes from PowerPoint and dump it into a table.

The line that I added was just the below and I've tried oSh.TextFrame oSh.TextRange and a combination with no luck. It returns a file that has headers but is completely blank. Any idea what I am doing wrong and why this won't work?

& oSh.Text & vbTab _

Full code:

Sub ExportCoords()

    Dim oSlides As Slides
    Dim oSl As Slide
    Dim oSh As Shape
    Dim strOutput As String
    Dim strFileName As String
    Dim intFileNum As Integer
    Dim lngReturn As Long

    ' Get a filename to store the collected text
    strFileName = InputBox("Enter the full path and name of file to save info to", "Output file?")

    ' did user cancel?
    If strFileName = "" Then
        Exit Sub
    End If

    ' is the path valid?  crude but effective test:  try to create the file.
    intFileNum = FreeFile()
    On Error Resume Next
    Open strFileName For Output As intFileNum
    If Err.Number <> 0 Then     ' we have a problem
        MsgBox "Couldn't create the file: " & strFileName & vbCrLf _
            & "Please try again."
        Exit Sub
    End If
    Close #intFileNum  ' temporarily

    strOutput = "Slide" & vbTab & "Name" & vbTab & "Text" & vbTab & "Type" _
    & vbtab & "Left" & vbTab & "Top" & vbTab & "width" _
    & vbTab & "height" & vbCrLf

    ' Get the info
    Set oSlides = ActivePresentation.Slides
    For Each oSl In oSlides
        For Each oSh In oSl.Shapes
            strOutput = strOutput _
                & oSl.SlideIndex & vbTab _
                & oSh.Name & vbTab _
                & oSh.Text & vbTab _
                & osh.Type & vbtab _
                & oSh.Left & vbTab _
                & oSh.Top & vbTab _
                & oSh.width & vbTab _
                & oSh.height & vbCrLf
        Next oSh
    Next oSl

    ' now write the text to file
    Open strFileName For Output As intFileNum
    Print #intFileNum, strOutput
    Close #intFileNum

    ' show what we've done
    lngReturn = Shell("NOTEPAD.EXE " & strFileName, vbNormalFocus)

End Sub

Sample Output: enter image description here

Upvotes: 1

Views: 1384

Answers (1)

Ivan
Ivan

Reputation: 446

Instead of oSh.Text, you should use oSh.TextFrame.TextRange.Text Then it will get the text inside the shape.

This happens because the TextFrame object has other properties besides the text value. For example, in the code below (from https://learn.microsoft.com/pt-br/office/vba/api/powerpoint.shape.textframe), you can set the margin and the text value.

Set myDocument = ActivePresentation.Slides(1)
With myDocument.Shapes _
        .AddShape(msoShapeRectangle, 180, 175, 350, 140).TextFrame
    .TextRange.Text = "Here is some test text"
    .MarginTop = 10
End With

Upvotes: 1

Related Questions