如何写VBA批量统计word和txt的字符数(不含空格)

我有大量的word文档和txt文档,估计有几千个,每个文档都要登记字符总数(不含空格),如果一个个打开实在太麻烦也很浪费时间;自己写的VBA好像不准确;请教各位大侠,有没有什么好的方法,或帮忙写个vba(指点下也可以);如果有现成的软件就更好了!请各位大侠帮忙

我把我以前用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
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-06
1个文字2个字节
1个数字1个字节
你就这样粗略计算,以百分比去分配下比率,以文件大小去计算
!要知道文件大小里面会包含一些外因的大小
相似回答