如何删除每张幻灯片上的文本框



我想删除每张幻灯片中的页码文本框,该框由10年前版本的MS PowerPoint创建,格式如下。

第1页,共47页

第一次尝试:

With ActivePresentation.Slides.Find
.Forward = True
.Wrap = wdFindStop
.Text = "*/47"
.Replacement.Text = ""
.Replace = wdReplaceAll
.MatchCase = False
End With

第二次尝试:

Sub ClNumbers()
Dim oSl As Slide
Dim oSh As Shape
Dim oTxtRng As TextRange
Dim sTextToFind As String
sTextToFind = "*/47"
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If InStr(oSh.TextFrame.TextRange.Text, sTextToFind) > 0 Then
Set oTxtRng = oSh.TextFrame.TextRange.Characters(InStr(oSh.TextFrame.TextRange.Text, sTextToFind), Len(sTextToFind))
Debug.Print oTxtRng.Text
With oTxtRng
.Font.Bold = True
End With
End If
End If
End If
Next
Next
End Sub

如何通过VBA删除所有页码?

(1(据我所知,VBA for Powerpoint中没有全局查找/替换。至少Application-对象、Presentation-对象或Slides-或Shapes集合没有Find-方法。由于编译器错误,您的尝试1失败。

(2(Powerpoint不支持通配符或常规expressen搜索。

(3(在您的第二次尝试中,您将用粗体标记文本,而不是删除形状或形状的文本(如果未找到(。

您需要循环浏览所有幻灯片的所有形状,并检查其中是否包含特定的文本模式。您的第二次尝试已接近尾声,但VBA函数InStr也不能使用通配符。相反,您可以使用VBALike-运算符。

您现在需要决定如何处理这些形状:
o您可以使用oSh.Delete完全删除它们
o您可以用oSh.Visible = False隐藏它们
>o您可以只使用oSh.TextFrame.TextRange.Characters.Delete删除文本

(4(如果在演示文稿的幻灯片母版上定义了形状,则代码将不会执行任何操作,因为幻灯片上根本没有这些形状。在这种情况下,只需在Powerpoint中编辑幻灯片母版-不需要任何代码。

所以你的代码可能看起来像你的第二次尝试,只需修改内部部分(并决定你想做什么(

If oSh.TextFrame.TextRange.Text Like sTextToFind Then
' oSh.Delete
' oSh.Visible = False
oSh.TextFrame.TextRange.Characters.Delete
End If

感谢FunThomas,它工作得很好。

这是代码:

Sub deletepagenumber()
' Remove the page number text box in each slide which was created by old version of MS powerpoint
' Before run this code, please check if the page number could be turned off in the slide master
' Thanks to FunThomas @ stackoverflow
Dim oSl As Slide
Dim oSh As Shape
Dim oTxtRng As TextRange
Dim sTextToFind As String
' Before run this code, please input the format of the page number, e.g. page ?/47, can be searched as "*/47"
sTextToFind = "*/47"
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.Text Like sTextToFind Then
' oSh.Delete
' oSh.Visible = False
oSh.TextFrame.TextRange.Characters.Delete
End If
End If
End If
Next
Next
End Sub

最新更新