我如何通过/复制相同的选择?这样我就可以通过单击在同一选择上运行多个宏。

时间:2018-12-19 16:18:30

标签: vba ms-word

我必须单击运行三个宏,当我调用第一个宏时,所选单词被逆转,但是选择丢失了,并且我认为由于单词的变化(反向),取消了对所选内容的选择,但是我需要选择才能在同一选择上运行其他宏。

Dim oWord As Range

If Selection.Information(wdWithInTable) = True Then
    For Each cl In Selection.Cells
        Set rng = cl.Range
        rng.MoveEnd Word.WdUnits.wdCharacter, Count:=-1
        For i = 1 To rng.Words.Count
            Set iRng = rng.Words(i)
            'rng.Select

            Set oWord = iRng
            Do While oWord.Characters.Last.Text = " "
                Call oWord.MoveEnd(WdUnits.wdCharacter, -1)
            Loop
            Debug.Print "'" & oWord.Text & "'"
            oWord.Text = StrReverse(oWord.Text)

            Debug.Print Selection.Text

        Next i
    Next cl
End If

End Sub
Sub Align()

'Selection.RtlPara
 Selection.LtrPara

 End Sub

 Private Sub CommandButton2_Click()

 Call Align
 Call CommandButton1_Click
 Call Comma_Remove
 Call CommandButton1_Click

 End Sub

 Sub Comma_Remove()


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = ","
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

此图显示了更改代码后我收到的问题

enter image description here

1 个答案:

答案 0 :(得分:1)

我修改了您的代码以向您展示我的意思。我添加了SelectedRange变量。选定范围使用重复项来复制选择范围。就您的代码而言,SelectedRange设置为全局变量。您在宏中复制了SelectedRange的宏中使用了localRange。

编辑2018-12-20:对代码进行小的更新,以添加选项显式,添加缺少的声明,将SelectedRange全局化,并使用代码将神秘的LtlPara替换为左对齐段落

编辑2018-12-21:修改后的代码以重新选择,并解释了这样做的必要性。

OP正在选择表中的一系列单元格,然后在单元格上进行迭代以对每个单元格中的字符串进行两次转换。通常,我们不会对所有这些操作都使用选择,而是设置一个字范围并使用该字范围。在这种特定情况下,这会引起问题,因为在应用于表时,选择和单词范围之间存在差异。区别在于Selection.Cells.Count给出选择中的单元格数量,而selection.Range.Cells.count给出表中的单元格数量,从Selection.range中的第一个单元格开始,对表中的每个单元格进行计数从左到右,逐行,直到到达选择中的最后一个单元格。这就是为什么在使用单词范围而不是选择时处理不在选择中的单元格的原因。

我们可以通过将选择范围保留在一个单词范围内,然后使用SelectedRange恢复选择来克服这种奇怪情况。为需要处理选择的每个子项选择Select。

Option Explicit
Public SelectedRange  As Word.Range

Private Sub CommandButton1_Click()

Dim cl As Word.Cell
Dim Rng As Word.Range
Dim i As Long
Dim iRng As Word.Range
Dim oWord As Word.Range

    SelectedRange.Select
    If Selection.Information(wdWithInTable) = True Then


        For Each cl In Selection.Cells
            Set Rng = cl.Range
            Rng.MoveEnd Word.WdUnits.wdCharacter, Count:=-1
            For i = 1 To Rng.Words.Count
                Set iRng = Rng.Words(i)
                'rng.Select

                Set oWord = iRng
                Do While oWord.Characters.Last.Text = " "
                    Call oWord.MoveEnd(WdUnits.wdCharacter, -1)
                Loop
                Debug.Print "'" & oWord.Text & "'"
                oWord.Text = StrReverse(oWord.Text)

                Debug.Print Selection.Text

            Next i
        Next cl
    End If

End Sub
Sub Align()
Dim localrange As Word.Range

    'Set localrange = SelectedRange.Duplicate
    SelectedRange.Select
    'Selection.RtlPara
    Selection.Paragraphs.Alignment = wdAlignParagraphLeft

End Sub

Private Sub CommandButton2_Click()
    Set SelectedRange = Selection.Range.Duplicate 'make a copy of the selection range
    Align
    CommandButton1_Click
    Comma_Remove
    CommandButton1_Click

End Sub

Sub Comma_Remove()

    SelectedRange.Select

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ","
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub