我只是在学习,还不知道多少。我写了错误的代码
Sub sierotkiTXT_select()
Do
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=wdExtend
If Selection.Text Like "* [aAwWzZiIoOuUVQ] *" Or Selection.Text Like "*[A-Z]. *" Or Selection.Text Like "* [a-z]. *" Or Selection.Text Like "*z. *" Or Selection.Text Like "*:] *" Then
Result = MsgBox("OK?", vbYesNoCancel + vbQuestion)
If Result = vbYes Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Delete
Selection.InsertAfter Text:=ChrW(160)
End If
If Result = vbCancel Then
Exit Sub
End If
End If
Selection.MoveRight Unit:=wdCharacter, Count:=3
Loop Until Selection.Text = ActiveDocument.Range.Characters.Last
End Sub
并且不知道如何在不使用的情况下在文档末尾停止这样的宏(打破循环(
Loop Until Selection.Text = ActiveDocument.Range.Characters.Last
这不是问题,但宏有时会停在段落字符的末尾,将它们解释为文档的末尾。[编辑]Ok-ActiveDocument.Range.Characters.Last仍然返回空-这就是它停止的原因。我不应该用这个。
示例(正文(:之前
运行宏后:之后
示例(尾注(:之前之后
您甚至不需要宏!您只需要两个通配符查找/替换操作,其中一个带有:
Find = (<[A-Za-z])^32
Replace = 1^s
一个带有:
Find = (<[a-z].)^32
Replace = 1^s
另请参阅:https://www.msofficeforums.com/word-vba/48717-how-i-find-character-line.html
我更正了我的代码,它就工作了。
Sub sierotkiTXT_select()
'szukaj sierotek
Dim NumLines As Long
Application.ScreenUpdating = True
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
NumLines = Selection.Range.ComputeStatistics(wdStatisticLines)
MsgBox "Lines to check " & (NumLines)
Selection.Collapse
For i = 1 To NumLines
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=wdExtend
If Selection.Text Like "* [aAwWzZiIoOuUVQ] *" Or Selection.Text Like "*[A-Z]. *" Or Selection.Text Like "* [a-z]. *" Or Selection.Text Like "*z. *" Or Selection.Text Like "*:] *" Then
Result = MsgBox("Akceptujesz?", vbYesNoCancel + vbQuestion)
If Result = vbYes Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Delete
Selection.InsertAfter Text:=ChrW(160)
End If
If Result = vbCancel Then
Exit Sub
End If
End If
Selection.MoveRight Unit:=wdCharacter, Count:=3
Next
End Sub
在我的例子中,我发现我可以使用搜索/替换功能而不是宏,但我可以使用,而不是在a、I、o、u、w、z之后插入每个硬空间(在我的情况下,这会使对齐的文本看起来很糟糕,并不必要地移动所有文本(
搜索:
空间
替换为:空格\ 1零宽度细木工空格
其中ZERO WIDTH JOINER是Unicode字符U+200D。如何在Microsoft Windows:Alt+(在NumPad键盘上(08205 中键入
ZERO WIDTH JOINER在WORD中是这样的(启用了"显示隐藏字符"选项(:零宽度托梁图像
下面是一个例子,说明在我的例子中,使用硬空格和ZERO WIDTH JOINER+空格键后文本的外观:示例