VBA - Powerpoint 根据“顶部”和“左侧”对文本框进行排序 属性
VBA - Powerpoint Sort Textboxes base on their “Top” and “Left” property
我在 powerpoint 幻灯片中有一堆文本框。
它们都包含文本。
我需要按顺序对这些文本框进行排序,
这样我就可以遍历那些文本框,
捕获文本,
并将其导出到 CSV 文件,按顺序从左上角到右下角。
例如,如果我在一张幻灯片中有 4 个文本框,我需要在
文本框,顺序为
- 左上角文本框
- 右上角文本框
- 左下文本框
- 右下文本框
将文本框的文本导出到 CSV 文件的代码部分(我从互联网上获得)有效。除了它们乱序。
Sub ExportTextToCSV()
Dim oPres As Presentation
Dim oSlides As Slides
Dim oSld As Slide 'Slide Object
Dim oShp As Shape 'Shape Object
Dim sTempString As String
Dim Quote As String
Dim Comma As String
Dim myText As String
Dim myFilePath As String
myFilePath = ".\Export_Textbox.CSV"
Quote = Chr$(34)
Comma = ","
Set oPres = ActivePresentation
Set oSlides = oPres.Slides
For Each oSld In oSlides 'Loop thru each slide
For Each oShp In oSld.Shapes 'Loop thru each shape on slide
'Check to see if shape has a text frame and text
If oShp.HasTextFrame And oShp.TextFrame.HasText Then
myText = Replace(oShp.TextFrame.TextRange.Text, vbCr, vbCrLf)
sTempString = sTempString & Quote & myText & Quote & Comma
End If
Next oShp
'Add new line in CSV
sTempString = sTempString & vbCrLf
'Print the result to file:
Call WriteToTextFileADO(myFilePath, sTempString, "UTF-8")
'Clear the string
sTempString = ""
Next oSld
End Sub
Sub WriteToTextFileADO(filePath As String, strContent As String, CharSet As String)
Set stm = CreateObject("ADODB.Stream")
'if file exist, append
If Len(Dir(filePath)) > 0 Then
stm.Type = 2
stm.Mode = 3
stm.Open
stm.CharSet = CharSet
stm.LoadFromFile filePath
stm.Position = stm.Size
stm.WriteText strContent
stm.SaveToFile filePath, 2
stm.Close
Else
stm.Type = 2
stm.Mode = 3
stm.Open
stm.CharSet = CharSet
stm.WriteText strContent
stm.SaveToFile filePath, 2
stm.Close
End If
Set stm = Nothing
End Sub
根据 Whosebug 的 post“VBA For each - loop order”,它说:
"A shape's position in the z-order corresponds to the shape's index
number in the Shapes collection."
我正在考虑首先创建并 运行 一个宏来 重新设置所有形状的 z 顺序,基于 "Top" 和 "Left" 属性 文本框形状 ,在我 运行 ExportTextToCSV() 宏之前。
我在使用 ShapeRange 或 Collection、在幻灯片中添加对现有形状的引用以及根据它们的 "Top" 和 "Left" 属性 对它们进行排序时遇到问题。
请帮忙。谢谢!
Create a disconnected recordset using ADO, populate it with textbox
name, text, top and left properties, then sort it by top then left
position. Use that to populate your text file. See for example:
developer.rhino3d.com/guides/rhinoscript/… – Tim Williams 23 hours ago
成功了。感谢您为我指明正确的方向!
如果您不介意,请重新post您的评论作为答案,以便我将其标记为答案。
我在 powerpoint 幻灯片中有一堆文本框。 它们都包含文本。
我需要按顺序对这些文本框进行排序, 这样我就可以遍历那些文本框, 捕获文本, 并将其导出到 CSV 文件,按顺序从左上角到右下角。
例如,如果我在一张幻灯片中有 4 个文本框,我需要在 文本框,顺序为
- 左上角文本框
- 右上角文本框
- 左下文本框
- 右下文本框
将文本框的文本导出到 CSV 文件的代码部分(我从互联网上获得)有效。除了它们乱序。
Sub ExportTextToCSV()
Dim oPres As Presentation
Dim oSlides As Slides
Dim oSld As Slide 'Slide Object
Dim oShp As Shape 'Shape Object
Dim sTempString As String
Dim Quote As String
Dim Comma As String
Dim myText As String
Dim myFilePath As String
myFilePath = ".\Export_Textbox.CSV"
Quote = Chr$(34)
Comma = ","
Set oPres = ActivePresentation
Set oSlides = oPres.Slides
For Each oSld In oSlides 'Loop thru each slide
For Each oShp In oSld.Shapes 'Loop thru each shape on slide
'Check to see if shape has a text frame and text
If oShp.HasTextFrame And oShp.TextFrame.HasText Then
myText = Replace(oShp.TextFrame.TextRange.Text, vbCr, vbCrLf)
sTempString = sTempString & Quote & myText & Quote & Comma
End If
Next oShp
'Add new line in CSV
sTempString = sTempString & vbCrLf
'Print the result to file:
Call WriteToTextFileADO(myFilePath, sTempString, "UTF-8")
'Clear the string
sTempString = ""
Next oSld
End Sub
Sub WriteToTextFileADO(filePath As String, strContent As String, CharSet As String)
Set stm = CreateObject("ADODB.Stream")
'if file exist, append
If Len(Dir(filePath)) > 0 Then
stm.Type = 2
stm.Mode = 3
stm.Open
stm.CharSet = CharSet
stm.LoadFromFile filePath
stm.Position = stm.Size
stm.WriteText strContent
stm.SaveToFile filePath, 2
stm.Close
Else
stm.Type = 2
stm.Mode = 3
stm.Open
stm.CharSet = CharSet
stm.WriteText strContent
stm.SaveToFile filePath, 2
stm.Close
End If
Set stm = Nothing
End Sub
根据 Whosebug 的 post“VBA For each - loop order”,它说:
"A shape's position in the z-order corresponds to the shape's index number in the Shapes collection."
我正在考虑首先创建并 运行 一个宏来 重新设置所有形状的 z 顺序,基于 "Top" 和 "Left" 属性 文本框形状 ,在我 运行 ExportTextToCSV() 宏之前。
我在使用 ShapeRange 或 Collection、在幻灯片中添加对现有形状的引用以及根据它们的 "Top" 和 "Left" 属性 对它们进行排序时遇到问题。
请帮忙。谢谢!
Create a disconnected recordset using ADO, populate it with textbox name, text, top and left properties, then sort it by top then left position. Use that to populate your text file. See for example: developer.rhino3d.com/guides/rhinoscript/… – Tim Williams 23 hours ago
成功了。感谢您为我指明正确的方向!
如果您不介意,请重新post您的评论作为答案,以便我将其标记为答案。