我必须单击一下运行三个宏,当我调用第一个宏时,所选单词被逆转,但是选择丢失了,我认为由于单词的更改而取消了选择(反向),但是我需要选择以在同一选择上运行其他宏。
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] 删除。
我来说两句