【问题标题】:(PPT VBA) Select textrange.characters failure(PPT VBA) 选择 textrange.characters 失败
【发布时间】:2021-06-06 19:09:01
【问题描述】:

我想在 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”总是显示错误

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

【问题讨论】:

  • 好诡异,我测试了一下却成功选中了文字
  • 我是 StackOverflow 的新访客。我不熟悉它。谢谢你,健祥。既然你能运行成功,我会尝试寻找其他可能的方法。
  • 不,我使用的是与您完全相同的代码,无需修改,但选择成功...

标签: vba select powerpoint


【解决方案1】:

如果你只引用当前的演示文稿而不是打开另一个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 Sub

【讨论】:

  • 我需要打开文件夹中的所有文件。代码: For Each f1 In fc If f1 Like "*.pptx" Then Set pres = Presentations.Open(filename:=f1) 我确定代码是正确的,因为我总是在其他潜艇中使用它们。
  • 也许您尝试打开特定文件并进行测试,因为我尝试打开文件没有问题。或者ppt之一是protected
  • 我刚刚发现有些形状可以工作,而有些形状不在同一个文件中。所以我想问题出在形状的属性上。我会继续努力。既然我知道了关键点,你知道如何结束这个问题吗?
  • 我明白了,很高兴您找到了根本原因。要关闭问题,您可以将其删除或接受我的回答:)
  • @Rachel1208 在 If sHp.HasTextFrame = msoTrue Then ... If sHp.TextFrame.HasText = True Then 之后添加另一个 if/then 测试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 2012-06-12
相关资源
最近更新 更多