我想在textrange中选择一些字符。当我使用"With activepresentation.slides(2).shapes(2)“时,它可以工作。代码:
dim Txtrng as textrange
dim Words_Instr as integer
dim aa as string
With ActivePresentation.Slides(2).Shapes(2)
Set Txtrng = .TextFrame.textRange
aa = "AAAA"
Words_Instr = InStr(Txtrng, aa)
If Words_Instr > 0 Then
Txtrng.Characters(Words_Instr, Len(aa)).Select
end if
end with当我使用"pres.“时,它不起作用。我想在每个幻灯片的每个形状上做同样的事情。代码:
dim pres as presentation
dim sli as slide
dim shp as shape
dim Txtrng as textRange
dim Words_Instr as integer
dim aa as string
set pres=Presentations.Open(filename:=f1)
aa = "AAAA"
For Each sLi In pRes.Slides
for each sHp in sLi.shapes
If sHp.HasTextFrame = msoTrue Then
Set Txtrng = sHp.TextFrame.textRange
Words_Instr = InStr(Txtrng, aa)
If Words_Instr > 0 Then
Txtrng.Characters(Words_Instr, Len(aa)).Select
end if
end if
next
next它总是在“txtrng.characters(...).select”中显示错误
如果有任何帮助,我将不胜感激。
发布于 2021-06-06 19:09:02
如果您只引用当前的演示文稿而不是打开另一个ppt,那么下面的代码就可以了,我只更改set pre部件,将此代码复制并粘贴到您现有的ppt中并运行它,它将选择形状上的text:
Sub test()
Dim pres As Presentation
Dim sli As Slide
Dim shp As Shape
Dim Txtrng As TextRange
Dim Words_Instr As Integer
Dim aa As String
Set pres = ActivePresentation
aa = "AAAA"
For Each sli In pres.Slides
For Each shp In sli.Shapes
If shp.HasTextFrame = msoTrue Then
Set Txtrng = shp.TextFrame.TextRange
Words_Instr = InStr(Txtrng, aa)
If Words_Instr > 0 Then
Txtrng.Characters(Words_Instr, Len(aa)).Select
End If
End If
Next
Next
End Subhttps://stackoverflow.com/questions/67856873
复制相似问题