我把我以前用excel做的类似的module改了下发给你
你可能需要在工具引用那里添加 microsoft word 14.0 object library
以及 microsoft scripting runtime。
你可以改下用word vba,然后txt那部分的处理用别的方法做就不用添加引用了
Sub CountTxtWord()
Dim fName As String, fPath As String
Dim s As String
Dim i As Integer
Dim WrdApp As Object
Set WrdApp = CreateObject("word.application")
ActiveSheet.Cells.Clear
fPath = "D:\test6\"
fName = Dir(fPath)
i = 2
Do Until fName = ""
If Right(fName, 4) = ".txt" Then
With New Scripting.FileSystemObject
With .OpenTextFile(fPath & fName)
s = .ReadAll
s = Replace(s, " ", "")
Cells(i, 1) = fPath & fName
Cells(i, 2) = Len(s)
i = i + 1
End With
End With
Else
WrdApp.Documents.Open (fPath & fName)
s = WrdApp.ActiveDocument.Range.Text
WrdApp.ActiveDocument.Close False
s = Replace(s, " ", "")
Cells(i, 1) = fPath & fName
Cells(i, 2) = Len(s)
i = i + 1
End If
fName = Dir
Loop
Columns(1).AutoFit
WrdApp.Quit
End Sub
温馨提示:答案为网友推荐,仅供参考