我有以下代码,可以在PPT幻灯片的正文中添加句点:
Sub TitlePeriod()
On Error Resume Next
Dim sld As Slide
Dim shp As Shape
Dim strTitle As String
For Each sld In ActivePresentation.Slides
If sld.Shapes.HasTitle = True Then 'check if there is a title
strTitle = sld.Shapes.Title.TextFrame.TextRange.Text
Else
strTitle = ""
End If
For Each shp In sld.Shapes
'add periods only if text of shape is not equal to title text.
If strTitle <> shp.TextFrame.TextRange.Text Then
shp.TextFrame.TextRange.AddPeriods
If shp.HasTable Then
shp.TextFrame.TextRange.AddPeriods
End If
End If
Next shp
Next sld
End Sub
我正在尝试将代码添加到代码中,以便为幻灯片中的表添加句点
If shp.HasTable Then
shp.TextFrame.TextRange.AddPeriods
当我运行代码时没有错误,但表中没有添加句点。会喜欢一些建议或任何有关如何解决此问题的提示。
提前致谢
答案 0 :(得分:2)
首先,我想提供一些建议。在尝试找出这样的问题时,最好尝试检查本地窗口中的对象。这样,您可以搜索对象的属性(在本例中,形状对象,shp,恰好是表),并确定需要修改哪些属性以获得所需的结果。没有冒犯意味着,但是从你的问题来看,你似乎是VBA的新手并在某个地方找到了一些代码。
此外,代码实际上导致了我的错误,因为Table形状没有文本框架(虽然我只做了一个测试表......也许你的实际上有一个)。我添加了textFrame的检查。
对于您的特定问题,带有表的形状对象具有需要用于向单元格添加内容的Table属性。反过来,表有一个Columns对象,它是一个列的集合。您需要循环遍历所有列。每列都是一组单元格,因此您需要遍历单元格。每个单元格都有您要查找的textframe和textrange对象,因此您需要对这些对象运行.AddPeriods方法。
Sub TitlePeriod()
On Error Resume Next
Dim sld As Slide
Dim shp As Shape
Dim strTitle As String
Dim myTable As Table
Dim myColumns As Columns
Dim col As Column
Dim myCell As Cell
For Each sld In ActivePresentation.Slides
If sld.Shapes.HasTitle = True Then 'check if there is a title
strTitle = sld.Shapes.Title.TextFrame.TextRange.Text
Else
strTitle = ""
End If
For Each shp In sld.Shapes
'add periods only if text of shape is not equal to title text.
If shp.TextFrame.HasText Then 'check to make sure there is text in the shape
If strTitle <> shp.TextFrame.TextRange.Text Then
shp.TextFrame.TextRange.AddPeriods
End If
End If
If shp.HasTable Then 'Check to see if shape is a table
Set myTable = shp.Table 'Get the table object of the shape
Set myColumns = myTable.Columns 'Get the columns of the table
For Each col In myColumns 'Loop through the columns
For Each myCell In col.Cells 'Loop through the cells in the column
myCell.Shape.TextFrame.TextRange.AddPeriods 'Add periods to the cell
Next myCell
Next col
End If
Next shp
Next sld
End Sub