|
- Sub main()
- Dim doc As Document, Rng As Range, RngS As Range, WordStr As String, reg As Object, Par As Paragraph
- Set doc = ActiveDocument
- Set reg = CreateObject("VBSCRIPT.REGEXP") 'RegEx为建立正则表达式
- With reg
- .Global = True '设置全局可用
- .Pattern = "[a-zA-Z]+"
- End With
- If Len(Selection.Range.Text) > 0 Then
- Set Rng = Selection.Range
- Do While Rng.Characters(1) = " " And Len(Rng.Text) > 0
- Rng.MoveStart wdCharacter, 1
- Loop
- If Len(Rng.Text) > 0 Then
- Rng.SetRange Rng.Words(1).Start, Rng.End
- Set RngS = doc.Range(Rng.End, Rng.End)
- Do While RngS.Start <> Rng.Start
- RngS.Move wdWord, -1
- WordStr = RTrim(RngS.Words(1).Text)
- If reg.test(WordStr) Then
- RngS.Words(1).Characters(Len(WordStr)).InsertAfter "(" & WordStr & ",长度为:" & Len(WordStr) & ")"
- End If
- Loop
- End If
- Else
- For Each Par In doc.Paragraphs
- Set Rng = doc.Range(Par.Range.End - 1, Par.Range.End - 1)
- Do While Rng.Start <> Par.Range.Start
- Rng.Move wdWord, -1
- WordStr = RTrim(Rng.Words(1).Text)
- If reg.test(WordStr) Then
- Rng.Words(1).Characters(Len(WordStr)).InsertAfter "(" & WordStr & ",长度为:" & Len(WordStr) & ")"
- End If
- Loop
- Next
- End If
- End Sub
复制代码
没时间 随便写了一下 你先参考一下吧 |
|