|
如图,我的VBA程序想做到检查RTF文档里是否有三线表跨页显示的情况。目前只是做到了检查每一个文档里页码数和表格数的比对,但是无法定位到具体是哪一页出现了跨页情况,测试文档和我的VBA程序已经上传,求大神帮忙看下,万分感谢!
1.
2.
3. 我的代码
- 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
- 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
- fSuff = Split(f.Name, ".")(1)
- If fSuff = "rtf" Then n = n + 1
- Next
-
- 'ThisDocument.Tables(1).Delete
- '插入表格
- Set myTable = ThisDocument.Tables.Add(Range:=ThisDocument.Range(Start:=0, End:=0), NumRows:=n + 1, NumColumns:=4)
-
- With ThisDocument.Tables(1)
- .Borders.InsideLineStyle = wdLineStyleSingle
- .Borders.OutsideLineStyle = wdLineStyleSingle
- End With
-
- With ThisDocument.Tables(1)
- .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:="标记"
- End With
-
- '实例化word对象
- On Error Resume Next
- Set docApp = CreateObject("Word.Application")
-
- i = 3
- If Len(FName) > 0 Then
- ' Dim oDoc As Document
- ' Set oDoc = Word.ActiveDocument
- ' Dim oT As Table
- docApp.Documents.Open (rtfPath & "" & FName)
- With ThisDocument.Tables(1)
- .Cell(Row:=2, Column:=1).Range.InsertAfter Text:=FName
- .Cell(Row:=2, Column:=2).Range.InsertAfter Text:=docApp.ActiveDocument.Tables.Count
- .Cell(Row:=2, Column:=3).Range.InsertAfter Text:=docApp.Selection.Information(wdNumberOfPagesInDocument)
- End With
- If docApp.ActiveDocument.Tables.Count <> docApp.Selection.Information(wdNumberOfPagesInDocument) 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
- 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)
- With ThisDocument.Tables(1)
- .Cell(Row:=i, Column:=1).Range.InsertAfter Text:=FName
- .Cell(Row:=i, Column:=2).Range.InsertAfter Text:=docApp.ActiveDocument.Tables.Count
- .Cell(Row:=i, Column:=3).Range.InsertAfter Text:=docApp.Selection.Information(wdNumberOfPagesInDocument)
- End With
- If docApp.ActiveDocument.Tables.Count <> docApp.Selection.Information(wdNumberOfPagesInDocument) 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
- 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
- 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
复制代码
|
|