|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 lingongzhe 于 2021-2-9 16:23 编辑
如图,我的VBA程序想做到检查RTF文档里是否有三线表跨页显示的情况。用循环table的方式检查具体是哪一页出现了跨页情况。但是当页数很大的时候,运行速度特别慢,例子class.rtf只是将近3000页,就已经要跑27分钟。如何能够提高效率和运行速度呢?
测试文档和我的VBA程序已经上传,求大神帮忙看下,万分感谢!
代码如下
- Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
- Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
- Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
- Private Const GWL_STYLE = (-16)
- Private Const WS_THICKFRAME As Long = &H40000 '(恢复大小)
- Private Const WS_MINIMIZEBOX As Long = &H20000 '(最小化)
- Private Const WS_MAXIMIZEBOX As Long = &H10000 '(最大化)
- Private Sub UserForm_Initialize()
- Dim hWndForm As Long
- Dim IStyle As Long
- hWndForm = FindWindow("ThunderDFrame", Me.Caption)
- IStyle = GetWindowLong(hWndForm, GWL_STYLE)
- IStyle = IStyle Or WS_THICKFRAME '还原
- IStyle = IStyle Or WS_MINIMIZEBOX '最小化
- IStyle = IStyle Or WS_MAXIMIZEBOX '最大化
- SetWindowLong hWndForm, GWL_STYLE, IStyle
- End Sub
- Private Sub CommandButton1_Click()
- Application.ScreenUpdating = True
- Application.DisplayAlerts = False
- Dim t As Table, c As Cell
- Dim f As Object
- Dim MyTable As Table
- Dim tabCount, pageCount, cellCount, firstCharPage, lastCharPage As Long
- startTime = Date + Time
- Selection.WholeStory
- Selection.Delete unit:=wdCharacter, Count:=1
- rtfPath = TextBox1.Value
- FName = Dir(rtfPath & "" & "*.rtf")
- n = 0
- For Each f In CreateObject("scripting.FileSystemObject").GetFolder(rtfPath).Files
- If f.Name Like "*.rtf" Then n = n + 1
- Next
-
- '插入表格
- Set MyTable = ThisDocument.Tables.Add(Range:=ThisDocument.Range(Start:=0, End:=0), NumRows:=n + 1, NumColumns:=5)
- With ThisDocument.Tables(1)
- .Columns(1).Width = 125
- .Columns(2).Width = 50
- .Columns(3).Width = 50
- .Columns(4).Width = 40
- .Columns(5).Width = 150
- .Borders.InsideLineStyle = wdLineStyleSingle
- .Borders.OutsideLineStyle = wdLineStyleSingle
- .Cell(Row:=1, Column:=1).Range.InsertAfter Text:="RTF"
- .Cell(Row:=1, Column:=2).Range.InsertAfter Text:="表格数"
- .Cell(Row:=1, Column:=3).Range.InsertAfter Text:="页码数"
- .Cell(Row:=1, Column:=4).Range.InsertAfter Text:="标记"
- .Cell(Row:=1, Column:=5).Range.InsertAfter Text:="跨页页码"
- End With
-
- '实例化word对象
- On Error Resume Next
- Set docApp = CreateObject("Word.Application")
-
- i = 3
- If Len(FName) > 0 Then
- docApp.Documents.Open (rtfPath & "" & FName)
- tabCount = docApp.ActiveDocument.Tables.Count
- pageCount = docApp.Selection.Information(wdNumberOfPagesInDocument)
- '检查跨页
- If tabCount <> pageCount Then
- For Each t In docApp.ActiveDocument.Tables
- cellCount = t.Range.Cells.Count
- firstCharPage = t.Range.Cells(1).Range.Characters.First.Information(wdActiveEndPageNumber)
- lastCharPage = t.Range.Cells(cellCount).Range.Characters.First.Information(wdActiveEndPageNumber)
- If firstCharPage <> lastCharPage Then
- With ThisDocument.Tables(1)
- .Cell(Row:=i, Column:=5).Range.InsertAfter Text:=firstCharPage & "; "
- End With
- End If
- Next
- End If
- With ThisDocument.Tables(1)
- .Cell(Row:=2, Column:=1).Range.InsertAfter Text:=FName
- .Cell(Row:=2, Column:=2).Range.InsertAfter Text:=tabCount
- .Cell(Row:=2, Column:=3).Range.InsertAfter Text:=pageCount
- End With
- If tabCount <> pageCount Then
- With ThisDocument.Tables(1)
- .Cell(Row:=2, Column:=4).Range.InsertAfter Text:="Y"
- End With
- With ThisDocument.Tables(1)
- .Cell(Row:=2, Column:=1).Range.Font.ColorIndex = wdRed
- .Cell(Row:=2, Column:=2).Range.Font.ColorIndex = wdRed
- .Cell(Row:=2, Column:=3).Range.Font.ColorIndex = wdRed
- .Cell(Row:=2, Column:=4).Range.Font.ColorIndex = wdRed
- .Cell(Row:=2, Column:=5).Range.Font.ColorIndex = wdRed
- End With
- End If
- pctDone = Format(1 / n, "0.0%")
- With UserForm1
- .Label2.Caption = 0
- .Label2.Caption = pctDone
- End With
- docApp.ActiveDocument.Close False
- Do
- FName = Dir
- If FName <> "" Then
- docApp.Documents.Open (rtfPath & "" & FName)
- tabCount = docApp.ActiveDocument.Tables.Count
- pageCount = docApp.Selection.Information(wdNumberOfPagesInDocument)
- '检查跨页
- If tabCount <> pageCount Then
- For Each t In docApp.ActiveDocument.Tables
- cellCount = t.Range.Cells.Count
- firstCharPage = t.Range.Cells(1).Range.Characters.First.Information(wdActiveEndPageNumber)
- lastCharPage = t.Range.Cells(cellCount).Range.Characters.First.Information(wdActiveEndPageNumber)
- If firstCharPage <> lastCharPage Then
- With ThisDocument.Tables(1)
- .Cell(Row:=i, Column:=5).Range.InsertAfter Text:=firstCharPage & "; "
- End With
- End If
- Next
- End If
- With ThisDocument.Tables(1)
- .Cell(Row:=i, Column:=1).Range.InsertAfter Text:=FName
- .Cell(Row:=i, Column:=2).Range.InsertAfter Text:=tabCount
- .Cell(Row:=i, Column:=3).Range.InsertAfter Text:=pageCount
- End With
- If tabCount <> pageCount Then
- With ThisDocument.Tables(1)
- .Cell(Row:=i, Column:=4).Range.InsertAfter Text:="Y"
- End With
- With ThisDocument.Tables(1)
- .Cell(Row:=i, Column:=1).Range.Font.ColorIndex = wdRed
- .Cell(Row:=i, Column:=2).Range.Font.ColorIndex = wdRed
- .Cell(Row:=i, Column:=3).Range.Font.ColorIndex = wdRed
- .Cell(Row:=i, Column:=4).Range.Font.ColorIndex = wdRed
- .Cell(Row:=i, Column:=5).Range.Font.ColorIndex = wdRed
- End With
- End If
- pctDone = Format(i / n, "0.0%")
- With UserForm1
- .Label2.Caption = 0
- .Label2.Caption = pctDone
- End With
- i = i + 1
- docApp.ActiveDocument.Close False
- End If
- Loop Until Len(FName) = 0
- End If
- With UserForm1
- .Label2.Caption = 100
- End With
- Set docApp = Nothing
- Application.DisplayAlerts = True
- endTime = Date + Time
- interval = Format(endTime - startTime, "hh:mm:ss")
- MsgBox "耗时" & interval
- MsgBox "Finished!", 64, " "
- End Sub
- Private Sub CommandButton2_Click()
- Set objshell = CreateObject("wscript.shell")
- Set DosExec = objshell.Exec("cmd.exe /c " & "taskkill /f /t /im WINWORD.exe")
- Set DosExec = Nothing
- Set objshell = Nothing
- End Sub
复制代码
|
|