作為標題,我想洗掉每張幻燈片中的頁碼文本框,它是由 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 洗掉所有頁碼。提前致謝。
請更正我的 vba 代碼或提供您的優雅方法。
uj5u.com熱心網友回復:
(1)據我所知,VBA 中沒有針對 Powerpoint 的全域查找/替換。至少-object 、-Object 或-or集合沒有Find
-method 。您的嘗試 1 因編譯??器錯誤而失敗。Application
Presentation
Slides
Shapes
(2) Powerpoint 不支持通配符或正則運算式搜索。
(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
uj5u.com熱心網友回復:
感謝 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/526509.html
標籤:vba微软幻灯片软件