Word VBA修剪功能

时间:2015-12-28 19:07:59

标签: vba word-vba trim

我想使用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语句之前修剪我的选择?此外,对于上面的示例,是否有更简单的方法来进行适当的区分大小写的更改?

1 个答案:

答案 0 :(得分: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

代码未经测试。任何疑问/澄清,只要问。