-5

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

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

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

4

1 回答 1

1

我从这个开始:

在此处输入图像描述

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

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

在此处输入图像描述


要遍历动态范围,试试这个。

在此处输入图像描述

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

在此处输入图像描述

于 2013-10-02T13:52:02.467 回答