VBA PowerPoint:从PowerPoint中提取形状文本

VBA PowerPoint:从PowerPoint中提取形状文本,vba,powerpoint,Vba,Powerpoint,我正在尝试修改下面现有的PowerPoint VBA代码,以便除了列出的其他属性之外,还包括形状文本。此代码的目的是从PowerPoint中提取每个形状/文本框及其属性,并将其转储到表中 我添加的行就在下面,我尝试了oSh.TextFrame oSh.TextRange和一个组合,但运气不好。它返回一个有标题但完全为空的文件。你知道我做错了什么,为什么这不起作用吗 & oSh.Text & vbTab _ 完整代码: Sub ExportCoords() Dim oS

我正在尝试修改下面现有的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
Sub ExportCoords()
将奥斯陆变暗为幻灯片
将oSl变暗为幻灯片
暗淡的oSh形状
作为字符串的暗输出
将strFileName设置为字符串
Dim intFileNum作为整数
暗变长
'获取文件名以存储收集的文本
strFileName=InputBox(“输入保存信息的文件的完整路径和名称”,“输出文件?”)
'用户取消了吗?
如果strFileName=“”,则
出口接头
如果结束
'路径有效吗?粗糙但有效的测试:尝试创建文件。
intFileNum=FreeFile()
出错时继续下一步
打开strFileName以作为intFileNum输出
如果错误号为0,则“我们有问题”
MsgBox“无法创建文件:”&strFileName&vbCrLf_
&“请再试一次。”
出口接头
如果结束
暂时关闭#intFileNum
strOutput=“Slide”&vbTab&“Name”&vbTab&“Text”&vbTab&“Type”_
&vbtab和“左”vbtab和“上”vbtab和“宽”_
&vbTab和“高度”及vbCrLf
'获取信息
设置oSlides=ActivePresentation.Slides
对于奥斯陆的每个oSl
对于oSl形状中的每个oSh
strOutput=strOutput_
&oSl.SlideIndex和vbTab_
&oSh.Name&vbTab_
&oSh.Text和vbTab_
&osh.Type&vbtab_
&oSh.左和右选项卡_
&oSh.Top和vbTab_
&oSh.width和vbTab_
&oSh高度和vbCrLf
下一个职业安全与健康
下一个奥斯陆
'现在将文本写入文件
打开strFileName以作为intFileNum输出
打印#intFileNum,strOutput
关闭#intFileNum
“展示我们所做的一切
lngReturn=Shell(“NOTEPAD.EXE”和strFileName,vbNormalFocus)
端接头
样本输出:

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

这是因为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),“”)\u根据版本的不同,它也会带来“换行符”。如果发生这种情况,您也可以删除那些替换Chr(10)的组件。参考: