首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(PPT VBA)选择textrange.characters故障

(PPT VBA)选择textrange.characters故障
EN

Stack Overflow用户
提问于 2021-06-06 15:49:50
回答 1查看 57关注 0票数 0

我想在textrange中选择一些字符。当我使用"With activepresentation.slides(2).shapes(2)“时,它可以工作。代码:

代码语言:javascript
复制
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.“时,它不起作用。我想在每个幻灯片的每个形状上做同样的事情。代码:

代码语言:javascript
复制
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”中显示错误

如果有任何帮助,我将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2021-06-06 19:09:02

如果您只引用当前的演示文稿而不是打开另一个ppt,那么下面的代码就可以了,我只更改set pre部件,将此代码复制并粘贴到您现有的ppt中并运行它,它将选择形状上的text

代码语言:javascript
复制
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 Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67856873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档