我根据守柔大侠的一处代码改写了一下,应该能初步达到目的,但我刚才试了一下,对一10页文件,费时太长,各位高手能否帮我优化一下,最好能提高效率和速度。
Sub TXT2EQ()
Dim SelStart As Long, SelRange As Range, NewSelStart As Long Dim i As Range, MyPost As Long On Error Resume Next Application.ScreenUpdating = False With ActiveDocument SelStart = .Start '定义一个起始位置 Set SelRange = .Range '定义一个区域为选定区域 End With While InStr(SelRange, "{") > 0 '设置如果指定区域中含有"{"则进入循环 NewSelStart = SelStart '定义一个开始位置 Set i = ActiveDocument.Range(NewSelStart, NewSelStart + 1) While i <> "}" '设置如果i的文本值不是为"}"则进入循环 Set i = ActiveDocument.Range(NewSelStart, NewSelStart + 1) '重新定义i的区域 NewSelStart = NewSelStart + 1 '将i的起点位置下移一个字符位置 If i = "{" Then MyPost = NewSelStart - 1 '取得当i="{"的位置 Wend '定义一个新区域 Set MyRange = ActiveDocument.Range(MyPost, NewSelStart) With MyRange .Characters(1) = "" '将{删除 .Characters(.Characters.Count) = "" '将}删除 .Select '选定 End With Application.Run "InsertFieldChars" '插入域标志 Wend Application.ScreenUpdating = True '恢复屏幕更新 End Sub
|