是否有可能知道excel中其他文件的字数?

时间:2013-10-02 13:35:38

标签: excel vba excel-vba

我想要一个包含.docx文件文件路径的Excel表。除此路径外,还应列出字数。 例如:

+----------------------------+------------+
| File Path                  | Word Count |
+----------------------------+------------+
| C:\MyDocs\summary.docx     | 42         |
+----------------------------+------------+
| C:\MyDocs\certificate.docx | 1337       |
+----------------------------+------------+

所以可以只在一个字段中写一个文件路径,而Excel只是用宏或类似的东西读出字数

1 个答案:

答案 0 :(得分:1)

我从这开始:

enter image description here

并使用此宏在下一列输出字数:

Sub GetMatchCount()
  Dim WordFileName As String

  WordFileName = Range("A1").Text

  With CreateObject("Word.Application")
    .Documents.Open (WordFileName)
    Text = .ActiveDocument.Words.Count
    .Quit
  End With

  Range("B1").Value = Text - 1
End Sub

enter image description here


要遍历动态范围,请尝试此操作。

enter image description here

Sub GetMatchCount()
  Dim numofrows As Integer
  numofrows = Sheet1.Range("A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp).Row

  Dim rng As Range
  Set rng = Range("A2:A" & numofrows)

  Dim WordFileName As String

  For Each cell In rng
    WordFileName = cell.Text

    With CreateObject("Word.Application")
      .Documents.Open (WordFileName)
      Text = .ActiveDocument.Words.Count
      .Quit
    End With

    cell.Offset(0, 1).Value = Text - 1
  Next
End Sub

enter image description here