【发布时间】:2015-03-07 03:46:20
【问题描述】:
我有大约 10 个标记为(1、2、3 等)的 Word 文档。我需要计算每个文档中的单词数和拼写错误的单词数,并将此计数输出到 Excel。
如何为此编写 VBA 脚本?
在 word 中,我知道如何找出字数和拼写错误的单词数并将其显示为 Msgbox,但我不确定如何让 Excel 读取 Word 文档并显示输出。
理想情况下,excel文件会要求我选择所有的word文档,然后生成一个表格,其中包含:
Doc Name Word_count Misspelled_count
1 30 9
2 45 8
3 50 15
.
.
我在 Word 中显示错误和字数的 VBA 代码是:
Sub get_wpm_errorcount()
Dim ThisDoc As Document
Dim ErrorCnt As Integer
Dim WordCnt As Integer
Set ThisDoc = ActiveDocument
WordCnt = ThisDoc.Range.ComputeStatistics(wdStatisticWords)
ErrorCnt = ThisDoc.SpellingErrors.Count
MsgBox ("WPM = " & WordCnt & " Error Count = " & ErrorCnt)
End Sub
编辑:
我试过了,这就是我想出的..
Dim RunErrorCount As Integer 'Global running total of errors
Dim FilesToOpen 'Global list of files to open
Dim FileCount As Integer 'Global count of files to open
Private Sub ReadtextFile()
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
On Error GoTo ErrHandler
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="All Files (*.*),*.*", _
MultiSelect:=True, Title:="Text Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
Call get_wpm_errorcount
Range("A1").Offset(FileCount + 1, 0).Select
Selection.NumberFormat = "0.00"
ActiveCell.Value = FilesToOpen(x).Name 'Output filename
Range("B1").Offset(FileCount + 1, i).Select
Selection.NumberFormat = "0.00"
ActiveCell.Value = WordCount 'Output total count
Range("C1").Offset(FileCount + 1, i).Select
Selection.NumberFormat = "0.00"
ActiveCell.Value = ErrorCount 'Output total count
x = x + 1
FileCount = UBound(FilesToOpen)
End Sub
Sub get_wpm_errorcount(ThisDoc As Object)
Dim ErrorCnt As Integer
Dim WordCnt As Integer
WordCnt = ThisDoc.Range.ComputeStatistics(wdStatisticWords)
ErrorCnt = ThisDoc.SpellingErrors.Count
End Sub
我做错了什么???
【问题讨论】: