2014-12-04 28 views
0

我有大約10個標記爲(1,2,3等)的Word文檔。我需要統計每個文檔中單詞的數量和拼錯單詞的數量,並將此計數輸出到Excel。計算單據中的單詞並輸出爲excel

如何爲此編寫VBA腳本?

在單詞中,我知道如何找出單詞計數和拼寫錯誤的單詞數,並將其顯示爲Msgbox,但我不確定如何使excel通過單詞文檔進行讀取並顯示輸出。

理想的情況下,Excel文件會問我選擇所有Word文檔,然後產生具有片:

Doc Name Word_count Misspelled_count 
1    30   9 
2    45   8 
3    50   15 
. 
. 

我的VBA代碼在Word中顯示錯誤和字數是:

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 

我在做什麼錯?

回答

3

應該很容易。根據需要修改。

您需要使用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 
+0

感謝您的幫助。我試圖綁定。我對VBA非常陌生,所以我仍然很難弄清楚這一點。 同樣,我希望Excel能夠讀取一堆Word文件。不只是一個具體的。然後我想在Excel中輸出。 – 2014-12-04 20:13:48

+0

查看將處理多個文件的修訂版。您將需要修改過程'get_wpm_errorCount'將輸出寫入Excel工作表,而不是在MessageBox中顯示。 – 2014-12-04 20:36:47

+0

您也可以使用'CreateObject(「Word.Application」)'而不設置對單詞的引用。 – 2014-12-04 20:37:32