目的是便于替换ppt中选中表格或文本框中的空格字符。
Sub ppt中替换选中对象空白字符()
Dim Shape As Shape
Dim searchStr As String
Dim replaceStr As String
' 设置要搜索和替换的文本
searchStr = " "
replaceStr = ""
' 遍历选中的所有形状
For Each Shape In ActiveWindow.Selection.ShapeRange
' 选中文本框处理
If Shape.Type = msoTextBox Then
Set TextBox = Shape.TextFrame.TextRange
TextBox.Text = Replace(TextBox.Text, searchStr, replaceStr)
End If
' 选中表格处理
If Shape.Type = msoTable Then
Set Tablecols = Shape.Table.Columns
Set Tablerows = Shape.Table.Rows
' 替换表格中列单元格的字符
For Each col In Tablecols
For Each ctext In col.Cells
TextBox = ctext.Shape.TextFrame.TextRange.Text
ctext.Shape.TextFrame.TextRange = Replace(TextBox, searchStr, replaceStr)
Next
Next
' 替换表格中行单元格的字符
For Each ro In Tablerows
For Each ctext In ro.Cells
TextBox = ctext.Shape.TextFrame.TextRange
ctext.Shape.TextFrame.TextRange = Replace(TextBox, searchStr, replaceStr)
Next
Next
End If
Next Shape
End Sub