【问题标题】:Word VBA Trim FunctionWord VBA 修剪功能
【发布时间】:2015-12-28 19:07:59
【问题描述】:

我想使用 Trim 或 Ltrim 来更改我的选择。我有一个长长的 If-Then 列表。以下是一个简短的例子

With Selection
If .Text = "Since" or .Text = "Since " Then .TypeText Text:= "Because"
If .Text = "since" or .Text = "since " Then .TypeText Text:= "because"
End With

如何在 if-then 语句之前修剪我的选择?另外,对于上面的示例,是否有更简单的方法来进行适当的区分大小写的更改?

【问题讨论】:

  • 应该是UCase(Trim(.Text)) = "SINCE"

标签: vba ms-word trim


【解决方案1】:

修剪部分:

If Trim(.Text) = "Since" Then .TypeText Text:= "Because"
If Trim(.Text) = "since" Then .TypeText Text:= "because"

对于大写字母部分:

也许您可以使用函数重新格式化每对 If-Then,如下所示:

CheckAndType("since", "because")

然后:

Function CheckAndType(origin as String, typeText as String)
    If Ucase(Trim(Selection.Text)) = UCase(Trim(origin)) Then
        If StrComp(Left(Selection.Text, 1), UCase(Left(Selection.Text, 1)), vbBinaryCompare) = 0 Then
            Selection.TypeText Text:=LCase(typeText)
        Else
            Selection.TypeText Text:= UCase(Left(typeText, 1)) & LCase(Right(typeText, Len(typeText) - 1)
        End If
    End If
End Function

代码未经测试。如有任何疑问/澄清,请询问。

【讨论】:

  • 这似乎会清理我的代码很多。我只是不明白如何实现它。创建函数后,我在哪里创建 CheckAndType ("since", "because") ("as", "because") 的列表
  • 谢谢。我制定了函数,现在我试图弄清楚如何在使用“调用 CheckAndType”列表中的特定单词时添加注释(通过调用宏)“调用 CheckAndType(“Since”,“因为") Application.Run MacroName:="Normal.NewMacros.CommentRW5"' 这没有按预期工作。
  • @WritePlace 调用你需要使用的宏Call NewMacros.CommentRW5
猜你喜欢
  • 2017-01-08
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 2014-07-25
相关资源
最近更新 更多