【问题标题】:Power point VBA, want to extract all words in all slidesPower point VBA,要提取所有幻灯片中的所有单词
【发布时间】:2014-09-03 20:04:12
【问题描述】:

我想更改以特定字符开头和结尾的特定单词的样式(例如!)

例如,其中一张幻灯片如下所示:


安静的地方

  • No1 吃
  • No2 说话
  • !不!睡觉

我有一个特定的字符!,表示要修改(将颜色更改为红色)

所以,我想我需要 VBA 中的函数:

strtok

读取所有字符串(可能在每张幻灯片中用空格''分隔)

输出:“安静”“地点”“No1”“吃”“No2”“说话”“!No3!” “睡觉”

strfind (strcmp)

对于每个单词,判断第一个和最后一个字符是否包含'!'

输出:“!No3!”

myfunc

对于以'!'开头和结尾的单词,修改它例如'bold'

这样的例子的结果是:


安静的地方

  • No1 吃
  • No2 说话
  • No3 睡觉

我从sberry找到了一个代码

Sub StrModify()
    Dim p As Presentation: Set p = ActivePresentation
    Dim s As Slide
    Dim sh As Shape
    For Each s In p.Slides
        For Each sh In s.Shapes
            If sh.HasTextFrame Then
                If sh.TextFrame.HasText Then
                    Debug.Print sh.TextFrame.TextRange.Text
                End If
            End If
        Next
    Next
End Sub

这个函数的输出是:

安静的地方

不吃东西

二话不说

!不!睡觉

不是由空格''标记的

是否有类似“strtok”的函数将“安静的地方”与“安静的”和“地方”分开?

【问题讨论】:

  • 我发现 WordArray = Split(sh.TextFrame.TextRange.Text, ' ') 完成了这项工作。但是如何访问 WordArray 中的单词并进行修改呢?

标签: vba powerpoint


【解决方案1】:

每个 TextRange 对象都有 Words、Lines、Paragraphs 和其他可以迭代的集合:

Dim oRng As TextRange
Dim oSh As Shape
Dim oSl As Slide
Dim x As Long

For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.Shapes
        If oSh.HasTextFrame Then
            If oSh.TextFrame.HasText Then
                For Each oRng In oSh.TextFrame.TextRange.Words
                    Debug.Print oRng.Text
                Next
            End If
        End If
    Next
Next

【讨论】:

    【解决方案2】:

    如果您打算使用 Steve 的(好的)建议来遍历 DONT use !作为标记。标点符号不被视为单词的一部分。

    【讨论】:

    • 好的,谢谢,约翰。忘了提那个。标点符号不会作为单词的一部分返回,因此您永远不会看到 !。您可以使用 xxxWordxxx 等,或者只使用 xxxWord 来标记需要特殊处理的单词。
    猜你喜欢
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 2019-11-15
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多