【问题标题】:VBA PowerPoint: Extract Shape Text from PowerPointVBA PowerPoint:从 PowerPoint 中提取形状文本
【发布时间】:2020-10-01 18:26:36
【问题描述】:

我正在尝试修改下面现有的 PowerPoint VBA 代码,以包括除了列出的其他属性之外的形状文本。此代码的目的是从 PowerPoint 中提取每个形状/文本框及其属性并将其转储到表格中。

我添加的行就在下面,我尝试了 oSh.TextFrame oSh.TextRange 和没有运气的组合。它返回一个有标题但完全空白的文件。知道我做错了什么以及为什么这不起作用吗?

& oSh.Text & vbTab _

完整代码:

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

样本输出:

【问题讨论】:

    标签: vba powerpoint


    【解决方案1】:

    您应该使用 oSh.TextFrame.TextRange.Text 而不是 oSh.Text 然后它将获取形状内的文本。

    这是因为 TextFrame 对象除了文本值之外还有其他属性。例如,在下面的代码中(来自https://docs.microsoft.com/pt-br/office/vba/api/powerpoint.shape.textframe),您可以设置边距和文本值。

    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
    

    【讨论】:

    • 这拉动了一切,但是当形状文本中包含多个段落时,它似乎正在挣扎(参见上面的屏幕截图)。知道为什么会这样吗?
    • 出现这种情况是因为你在构建txt的时候,如果文本有多个段落,会在文本中带上“回车”字符。如果你想避免这种情况,你可以使用: & Replace(oSh.TextFrame.TextRange.Text, Chr(13), "") _
    • 可能根据版本,它也会带上“换行”字符。如果发生这种情况,您也可以删除替换 Chr(10) 的那些。参考:pt.wikipedia.org/wiki/Ficheiro:ASCII-Table-wide.svg
    猜你喜欢
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2011-06-08
    • 2022-09-24
    • 2019-01-13
    • 2013-10-01
    • 2020-04-24
    • 1970-01-01
    相关资源
    最近更新 更多