【问题标题】:loop through words VBA powerpoint遍历单词 VBA PowerPoint
【发布时间】:2014-08-26 03:34:20
【问题描述】:

我是 powerpoint 对象模型的新手,想知道为什么下面的 sn-p 代码不起作用。代码的目的是通过一个文本框并用它们的小写等价词替换某些单词。我不确定我是否要正确替换这个词......但我很困惑为什么程序不会“输入”if语句......即使它找到和“of”并且它们都是字符串!

感谢任何帮助! :)

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

【问题讨论】:

    标签: vba loops powerpoint words


    【解决方案1】:

    尝试使用 strcomp 函数,例如。

    Dim str1 as String = "of"
    strcomp(CStr(LCase(theWord)), str1)
    

    http://msdn.microsoft.com/en-us/library/9s233cfc(v=vs.90).aspx

    【讨论】:

      【解决方案2】:

      为什么下面的 sn-p 代码不起作用。

      代码的 sn-p 正在工作,只是没有按照您的预期进行 :)

      ...因为每个theWord 可能包含尾随空格:"of " "of"

      为了处理这种可能性,还有Trim 函数(你可以省略Cstr,因为这在这里没有任何作用):

       If LCase(Trim(theWord)) = "of" Then 
           theWord.Text = LCase(theWord) 
       End If
      

      所以,我们比较 修剪 字符串,然后我们替换整个字符串,这会保留尾随空格。

      您可能还对 TextRange.Replace 方法感兴趣,正如 Steve Rindsberg 在 cmets 中提到的那样。

      【讨论】:

      • 请注意,您根本不需要 Cstr。它只是将字符串转换为字符串。没必要。您可能还想查看 .TextRange.Replace 方法,该方法将保留替换文本的任何格式。
      猜你喜欢
      • 2017-05-14
      • 2021-12-12
      • 1970-01-01
      • 2013-10-09
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 2010-11-20
      相关资源
      最近更新 更多