|
* 楼主,我昨天下午重新写了 3 种跳过表格只处理文字的代码,请参考:
- Sub a计算宏运行所用时间()
- Dim t As Single
- t = Timer
- test1 '宏名-------test1=.101'---->第一种方法最快!
- MsgBox "OK!用时 " & Timer - t & " 秒!", vbOKOnly + vbExclamation, "计算宏运行所用时间"
- End Sub
- Sub test1()
- '目标:设置表格外文字为红色!
- Dim d As Document
- Set d = ActiveDocument
- '方法1:Do...Loop逐段循环------用时 .109/.101/.104秒
- ' On Error Resume Next
- d.Content.InsertAfter Text:="`"
- d.Paragraphs(1).Range.Select
- With Selection
- Do
- If Selection Like "*`*" Then Exit Do
- If .Information(wdWithInTable) = False Then
- .Font.Color = wdColorRed
- .Next(unit:=wdParagraph, Count:=1).Select
- Else
- .Tables(1).Range.Next(unit:=wdParagraph, Count:=1).Select
- End If
- Loop
- End With
- End Sub
- Sub test2()
- '方法2:独创/原创-----------用时:.519/.531/.539秒
- On Error Resume Next
- Dim doc As Document, t As Table, i As Paragraph, j&, k&, r As Range, v&
- Set doc = ActiveDocument
- If doc.Paragraphs(1).Range.Information(wdWithInTable) = False Then
- Selection.HomeKey unit:=wdStory
- doc.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:=1
- v = 1
- End If
- For Each t In doc.Tables
- With t.Range
- .Rows.WrapAroundText = False
- .Rows.Alignment = wdAlignRowCenter
- .Previous(unit:=wdParagraph, Count:=1).Characters.Last.InsertBefore Text:="`"
- End With
- Next
- doc.Content.InsertAfter Text:="`"
- k = doc.Tables.Count
- Do
- j = j + 1
- doc.Tables(j).Range.Next(unit:=wdParagraph, Count:=1).Characters(1).Select
- Selection.MoveEndUntil cset:="`", Count:=wdForward
- Selection.MoveEnd unit:=wdCharacter, Count:=2
- Selection.Font.Color = wdColorRed
- Selection.Characters.Last.Previous.Delete
- Loop Until j = k
- If v = 1 Then doc.Tables(1).Delete
- End Sub
- Sub test3()
- '方法3:循环遍历段落法------用时:.941/.906/.910秒
- Dim i As Paragraph
- For Each i In ActiveDocument.Paragraphs
- If i.Range.Information(wdWithInTable) = False Then i.Range.Font.Color = wdColorRed
- Next
- End Sub
复制代码 |
|