我是powerpoint对象模型的新手,我想知道为什么下面的代码片段不起作用。代码的目的是遍历文本框架,并将某些单词替换为与其对应的小写字母。我不确定我是否要替换单词correctly...but我最困惑的是为什么程序在找到和"of“时不能”输入“if statement...even,而且它们都是字符串!
感谢您的帮助!:)
Sub findConjectures()
Dim theWord As TextRange
With ActiveWindow.Selection.ShapeRange(1).TextFrame
For Each theWord In .TextRange.Words
MsgBox CStr(theWord) 'just used this as a test
If CStr(LCase(theWord)) = "of" Then ' this is the part that confuses me!
theWord.Text = LCase(theWord) 'not sure if this is used correctly
End If
Next
End With
End Sub发布于 2014-07-05 04:39:53
尝试使用strcomp函数,例如。
Dim str1 as String = "of"
strcomp(CStr(LCase(theWord)), str1)http://msdn.microsoft.com/en-us/library/9s233cfc(v=vs.90).aspx
发布于 2014-07-06 00:15:01
为什么下面这段代码不能工作。
这段代码正在工作,只是没有做您期望的事情:)
...because每个theWord可以包含尾随空格:"of " <> "of"
为了处理这种可能性,还可以使用Trim函数(您可以省略Cstr,因为它在这里没有任何影响):
If LCase(Trim(theWord)) = "of" Then
theWord.Text = LCase(theWord)
End If因此,我们比较修剪后的字符串,然后替换整个字符串,这保留了尾随空格。
正如Steve Rindsberg在评论中提到的那样,您可能还对TextRange.Replace方法感兴趣。
https://stackoverflow.com/questions/24580324
复制相似问题