Word VBA中的退格键

时间:2016-01-04 23:07:16

标签: vba ms-word word-vba

我的功能如下:

Function Replace(source As String, typeText As String)
    If Trim(Selection.Text) = Trim(source) Then
    Selection.typeText Text:=typeText
    End If
End Function

我运行了一长串的Call Replace行,但偶尔我需要在替换文本之前键入一个或两个退格。例如,如果我[因此]突出显示“她很冷,因此,她穿上夹克”,那么我需要用[因此]替换[;]因此,]。

With Selection
    Call Replace("therefore", "; therefore,")
End with

显然,这会留下两个不正确的逗号和一个空格。

1 个答案:

答案 0 :(得分:0)

这会做你所问的,虽然很脏但

Sub SomeSub()

    Dim MyString As String

                        'therefore is case sensitive
    If Selection.Text = "therefore" Then

        Selection.MoveStart Unit:=wdCharacter, Count:=-2
        Selection.MoveEnd Unit:=wdCharacter, Count:=1

        MyString = Selection

        If InStr(String1 = MyString, String2 = ",") = 1 And _
            InStrRev(String1 = MyString, String2 = ",") = 1 Then

            'changin text to requirmeent
            Selection.typeText "therefore"
            Selection.MoveLeft wdWord, 1, wdExtend
        Else
            'resetting original selection
            Selection.MoveStart Unit:=wdCharacter, Count:=2
            Selection.MoveEnd Unit:=wdCharacter, Count:=-1

        End If

        With Selection
            Call Replace("therefore", "; therefore,")
        End With
    End If
End Sub
相关问题