Vba 计算文档中的字数并输出到excel

Vba 计算文档中的字数并输出到excel,vba,excel,ms-word,Vba,Excel,Ms Word,我有大约10个Word文档(1、2、3等)。我需要计算每个文档中的字数和拼写错误的字数,并将此计数输出到Excel 如何为此编写VBA脚本 在word中,我知道如何找出字数和拼写错误的字数,并将其显示为Msgbox,但我不知道如何让excel读取word文档并显示输出 理想情况下,excel文件将要求我选择所有word文档,然后生成具有以下内容的工作表: Doc Name Word_count Misspelled_count 1 30 9

我有大约10个Word文档(1、2、3等)。我需要计算每个文档中的字数和拼写错误的字数,并将此计数输出到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

我做错了什么?

应该很容易。根据需要修改

您需要使用Excel打开(然后控制)Word。这称为“绑定”。下面的示例将Word绑定到Excel:

Option Explicit
'NOTE: REQUIRES REFERENCE TO MS WORD
Sub main()
Dim wordApp as Word.Application
Dim wordDoc as Word.Document
Dim fd As FileDialog
Dim listFiles As Variant
Dim i As Long

'Display the file dialog to allow the user to select file(s)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.AllowMultiSelect = True
    fd.ButtonName = "Select Files to Process"
    fd.Show

'Create Word Application only if needed:
If fd.SelectedItems.Count > 0 Then Set wordApp = CreateObject("Word.Application")

'Then iterate th selected items and open them in Word:
For i = 1 to fd.SelectedItems.Count

    Set wordDoc = wordApp.Documents.Open(fd.SelectedItems(i))  


    'Send the document object AND the integer i to the other function
    ' which will read the document and print results to Excel spreadsheet
    Call get_wpm_errorcount(wordDoc, i)

    wordDoc.Close
Next

Set wordApp = Nothing


End Sub

Sub get_wpm_errorcount(ThisDoc As Object, itemNumber as Integer)
Dim ErrorCnt As Integer
Dim WordCnt As Integer

WordCnt = ThisDoc.Range.ComputeStatistics(wdStatisticWords)
ErrorCnt = ThisDoc.SpellingErrors.Count

Range("A1").Offset(itemNumber, 0).Value = WordCnt
Range("B1").Offset(itemNumber, 0).Value = ErrorCnt

End Sub

应该很容易。根据需要修改

您需要使用Excel打开(然后控制)Word。这称为“绑定”。下面的示例将Word绑定到Excel:

Option Explicit
'NOTE: REQUIRES REFERENCE TO MS WORD
Sub main()
Dim wordApp as Word.Application
Dim wordDoc as Word.Document
Dim fd As FileDialog
Dim listFiles As Variant
Dim i As Long

'Display the file dialog to allow the user to select file(s)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.AllowMultiSelect = True
    fd.ButtonName = "Select Files to Process"
    fd.Show

'Create Word Application only if needed:
If fd.SelectedItems.Count > 0 Then Set wordApp = CreateObject("Word.Application")

'Then iterate th selected items and open them in Word:
For i = 1 to fd.SelectedItems.Count

    Set wordDoc = wordApp.Documents.Open(fd.SelectedItems(i))  


    'Send the document object AND the integer i to the other function
    ' which will read the document and print results to Excel spreadsheet
    Call get_wpm_errorcount(wordDoc, i)

    wordDoc.Close
Next

Set wordApp = Nothing


End Sub

Sub get_wpm_errorcount(ThisDoc As Object, itemNumber as Integer)
Dim ErrorCnt As Integer
Dim WordCnt As Integer

WordCnt = ThisDoc.Range.ComputeStatistics(wdStatisticWords)
ErrorCnt = ThisDoc.SpellingErrors.Count

Range("A1").Offset(itemNumber, 0).Value = WordCnt
Range("B1").Offset(itemNumber, 0).Value = ErrorCnt

End Sub

谢谢你的帮助。我试着装订。我对VBA非常陌生,所以我仍然很难弄清楚这一点。同样,我希望Excel能够读取大量Word文件。不仅仅是一个具体的问题。然后我想在Excel中输出..查看将处理多个文件的修订版。您需要修改过程
get\u wpm\u errorCount
以将输出写入Excel工作表,而不是显示在MessageBox中。您也可以使用
CreateObject(“Word.Application”)
而不设置对Word的引用。@因此,可以这样做。但是如果没有参考:)早期绑定通常对初学者来说更容易,尤其是因为intellisense。@david zemens为了打印到excel,我是否声明I为全局变量,然后将其添加到
get_wpm_errorCount
范围(“A1”)。偏移量(I,0).Value=WordCnt范围(“B1”).Offset(i,0.Value=ErrorCnt
)谢谢您的帮助。我试着装订。我对VBA非常陌生,所以我仍然很难弄清楚这一点。同样,我希望Excel能够读取大量Word文件。不仅仅是一个具体的问题。然后我想在Excel中输出..查看将处理多个文件的修订版。您需要修改过程
get\u wpm\u errorCount
以将输出写入Excel工作表,而不是显示在MessageBox中。您也可以使用
CreateObject(“Word.Application”)
而不设置对Word的引用。@因此,可以这样做。但是如果没有参考:)早期绑定通常对初学者来说更容易,尤其是因为intellisense。@david zemens为了打印到excel,我是否声明I为全局变量,然后将其添加到
get_wpm_errorCount
范围(“A1”)。偏移量(I,0).Value=WordCnt范围(“B1”)。偏移量(i,0)。Value=ErrorCnt