|
这是我的代码,不需要添加标点符号,你可以试试效果:
- Sub 返回只有一个字的行的数量()
- Dim p As Paragraph
- Dim n As Long
- For Each p In ActiveDocument.Content.Paragraphs
- p.Range.Select
- With Selection
- .Collapse 0
- .MoveLeft Unit:=wdCharacter, count:=1, Extend:=wdMove
- .HomeKey Unit:=wdLine, Extend:=wdExtend
- If 汉字数量(.Range.text) <= 1 Then
- .Range.Font.ColorIndex = 3
- n = n + 1
- End If
- End With
- Next
- MsgBox "只有一个汉字的行数:" & n
- End Sub
- Function 汉字数量(inputString As String) As Long
- Dim regEx As Object
- Dim matches As Object
- Dim count As Long
-
- ' 创建正则表达式对象
- Set regEx = CreateObject("VBScript.RegExp")
-
- ' 设置正则表达式模式为匹配汉字
- regEx.Pattern = "[\u4E00-\u9FFF]"
- regEx.Global = True
-
- ' 执行匹配
- Set matches = regEx.Execute(inputString)
-
- ' 汉字数量即为匹配的数量
- count = matches.count
-
- 汉字数量 = count
- End Function
复制代码 另外,代码放在Normal中,所以Word文档都可用,如果只放在某一个文档中,只有该文档可用
|
|