word如何提取某几段首尾加中括号?请问:一个word里有100段话,如何给不含有:(冒号)的那几段首尾统一加上中括号【】?

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/01 05:29:03

word如何提取某几段首尾加中括号?请问:一个word里有100段话,如何给不含有:(冒号)的那几段首尾统一加上中括号【】?
word如何提取某几段首尾加中括号?
请问:一个word里有100段话,如何给不含有:(冒号)的那几段首尾统一加上中括号【】?

word如何提取某几段首尾加中括号?请问:一个word里有100段话,如何给不含有:(冒号)的那几段首尾统一加上中括号【】?
'如果段落过长程序运行时间可能很长,注意看状态栏写的进度,代码在2013版Word初步测试无误,其余版本若存在不兼容问题请追问
'代码导入方法:
'打开你要操作的Word文档主UI按{Alt+F11}进入VBE,在左边的任意位置右键单击鼠标选择插入、模块,将此代码复制进去,关闭VBE,按{Alt+F8}选择Test并运行
'注意事项:程序运行完成会弹出End的对话框,此对话框弹出之前勿在Word中单击鼠标,可以将其最小化等待(标题栏可以单击),否则程序可能出现不可预知的错误
Sub test()
    Dim para As String, length As Long, i As Long, f As Field, j As Long, h As Boolean
    Application.ScreenUpdating = False
    Application.StatusBar = "Preparing"
    For j = 1 To ActiveDocument.Paragraphs.Count
        Application.StatusBar = Int(j / (ActiveDocument.Paragraphs.Count / 100)) & " percent finished"
        para = ActiveDocument.Paragraphs(j).Range
        length = Len(para)
        h = False
        For i = 1 To length - 1
            If Mid(para, i, 1) = ":" Then
                h = True
                Exit For
            End If
        Next
        If h = True Then
            ActiveDocument.Paragraphs(j).Range.Select
            Selection.MoveLeft , 1
            Selection.TypeText "【】"
            ActiveDocument.Paragraphs(j).Range.Select
            Selection.MoveRight , 1
            Selection.MoveLeft , 1
            Selection.TypeText "【】"
        End If
    Next
    Application.StatusBar = "Finishing"
    Application.ScreenUpdating = True
    Application.StatusBar = "End"
    MsgBox "End!", vbInformation, "test"
End Sub