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

M周日

我必须单击一下运行三个宏,当我调用第一个宏时,所选单词被逆转,但是选择丢失了,我认为由于单词的更改而取消了选择(反向),但是我需要选择以在同一选择上运行其他宏。

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

此图说明了更改代码后我收到的问题

在此处输入图片说明

自由流动

I amended your code to show you what I mean. I added the SelectedRange variable. Selected range uses duplicate to make a copy of the selection range. SelectedRange is set as a global variable for the purposes of your code. localRange is used in your macros where we make a copy of SelectedRange.

Edit 2018-12-20: Minor updates to the code to add option explicit, add missing declarations, make SelectedRange global and to replace the mysterious LtlPara with code to left align paragraphs

Edit 2018-12-21: revised code to reestablish selection and an explanation of why this is necessary.

The OP is selecting a range of cells in a table and then iterating over the cells to do a couple of transformations on the string in each cell. Typically we would not use selection for all of these operations, instead we would set a word range and work with the word range. In this specific case this runs into a problem because there is a difference between Selection and a word range when applied to a table. The difference is that Selection.Cells.Count gives the number of of cells in the selection but selection.Range.Cells.count gives the number of cells in the table starting with the first cell in Selection.range, counting each cell in the table from left to right, row by row, until the last cell in the selection is reached. This is why cells not in the selection are being processed when using a word range rather than the selection.

我们可以通过将选择范围保留在单词范围内,然后使用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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过单击链接登录到Google Apps(这样我就可以在帐户之间切换)

我如何在constraintlayout创建一个容器,这样我就可以设置背景?

从Excel到Word选择VBA粘贴形状,这样我就可以将文本紧紧包裹

如何fread和fread endianness独立整数,这样我就可以在许多机器上fread和fread并始终得到相同的结果

在一个变量中对多个观察进行分类,这样我就可以将它们归类在新的专栏中。如何使代码更短?在R中

我想在模态出现时自动选择模态输入字段,这样我就可以直接输入它而无需将光标放在字段中

如何将R.drawable作为参数传递,这样我就可以解析图像

我们如何不用引用就可以通过引用将数组传递给函数

我在div元素上实现选择/取消选择。我可以一键选择div,但不能再次单击同一div取消选择它?

我可以从同一列中选择多个值

UseQuery graphql -> useState with useEffect 这样我就可以运行第二个 useEffect 来设置更多状态

将数据从Python程序保存到“工作区”,这样我就可以绘图而不必再次运行该程序

如何在ng-repeat中使用zingchart,这样我就可以在不同的图表上显示不同的数据。有什么办法吗

如何从xampp安装链接到php,这样我就可以使用php命令而不是完整路径了?

工厂课程,这样我就可以改变我的其他客户

我想通过使用扩展选择参数来选择多个选项,并从这些选项中我想通过传递一个匹配项来运行作业

只需单击一下按钮,就可以将我的 db on rails 中的内容减一

如何在xml的输出中分配到分支的链接,以便一旦我在html的输出中单击它就可以打开它

当同一页面中有多个具有相同类的元素时,如何通过类选择 id

如何制作它,以便仅通过在控制台中键入其名称就可以运行特定程序?

使ajax调用重复功能,这样我就可以再次调用它n

Fabric:处理异常,这样我就可以在try块中捕获它

在rgb()中使用命名的r颜色,这样我就可以添加alpha值

正确的实例化类的方法,这样我就可以验证证书中的路径链

在 MySql 查询中替换“OR EXISTS”,这样我就可以获得更好的性能结果

我可以让 div 只显示 svg 图像的内容,否则是透明的,这样我就可以将两个 svg 堆叠在一起吗?

我希望有人可以指导我,这样我就可以熟练掌握 c

在Outlook中,是否可以在我手动选择的电子邮件上运行宏?

是否有可能复制JVM,这样我就可以简单地从主jvm切换到辅助jvm,以防主jvm崩溃