VBA在 PowerPoint中的搜索和替换对不可编辑的文字艺术字返回错误
以下函数是用PowerPoint VBA编写的一个宏的一部分,该宏对一个文件夹中的所有文件执行全局搜索和替换。该函数在一系列测试文件上工作,这些测试文件包含各种不同的对象,其中包括WordArt的示例。然而,当我将其应用到生产文件时,它在遇到的第一个WordArt对象处卡顿,返回错误:-2137024809(80070057),“指定的值超出范围”。
对错误进行研究,表明我试图编辑一个没有文本框的对象上的文本。你可以看到我弹出的消息框显示文本框中有文本,但下一行的替换命令会导致上述错误。
进一步查看,我发现这张幻灯片上的所有对象都不可编辑。检查这张幻灯片的选择窗格,以及幻灯片母版,一切显示为未锁定。对象上方没有其他层。我可以将光标放在文本中并选中它。幻灯片中的其他部分都可以正常编辑。我甚至检查了文件是否被标记为只读。只是这张幻灯片上的对象……在数百个演示文稿中都存在相同的情况。
是什么原因导致这张幻灯片上的对象不可编辑……我该如何让我的宏函数仍然编辑它们?
Sub ProcessDocument(FilePath As String)
Dim ppDoc As Object
Dim ppApp As Object
'MsgBox FilePath
Set ppApp = GetObject(, "PowerPoint.Application")
Set ppDoc = ppApp.Presentations.Open(FilePath)
'Application.StatusBar = ppDoc
Dim sld As Slide
Dim shp As Shape
For Each sld In ppDoc.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
Set textLoc = shp.TextFrame.TextRange.Find("AAAA")
If Not (textLoc Is Nothing) Then
MsgBox shp.TextFrame.TextRange.Text
shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "AAAA", "----")
End If
End If
End If
Next shp
Next sld
' Save and close the document
ppDoc.Save
ppDoc.Close
End Sub
有什么线索表明问题可能出在哪里?或调试时有什么建议?
解决方案
在经过相当多的研究和试验之后,我发现了问题。看起来PowerPoint中单个对象在外观上显示为解锁,实际上是被锁定,不能编辑。
为了解决这个问题,在编辑之前,我简单地先锁定对象,然后立即解锁。
Sub ProcessDocument(FilePath As String)
Dim ppDoc As Object
Dim ppApp As Object
'MsgBox FilePath
Set ppApp = GetObject(, "PowerPoint.Application")
Set ppDoc = ppApp.Presentations.Open(FilePath)
'Application.StatusBar = ppDoc
Dim sld As Slide
Dim shp As Shape
For Each sld In ppDoc.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
shp.Locked = True 'Lock the object
shp.Locked = False 'Unlock the object - clears a lock even if the lock
'was in place before the above lock
Set textLoc = shp.TextFrame.TextRange.Find("AAAA")
If Not (textLoc Is Nothing) Then
shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "AAAA", "----")
End If
End If
End If
Next shp
Next sld
' Save and close the document
ppDoc.Save
ppDoc.Close
End Sub
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。