单词宏来计算引号中的单词

时间:2017-10-12 13:47:21

标签: vba ms-word word-vba word-count

我正在写大学的论文,我不允许在我的总字数中加入引号中使用的单词。由于Word没有这样做的功能,我希望有人能够通过创建宏来帮助我。我之前使用过宏,但是对于像这样复杂的东西(如果它甚至那么复杂)我的经验非常非常少。

我已经有类似于在整个文档中使用引用的东西,所以拥有这两个将是一个很大的帮助。我将在下面复制此代码,以便您可以大致了解我需要的内容,除了引号而不是引用。

所以我想知道是否有人能够生成一个宏来计算整个文档中引号中使用的单词数量?

Sub CitationWordCount()

Dim Fld As Field, l As Long, StrTmp As String
For Each Fld In ActiveDocument.Fields
  With Fld
    If .Type = wdFieldCitation Then
      StrTmp = .Result
      l = l + UBound(Split(StrTmp, " ")) + UBound(Split(StrTmp, "-")) + 1
      StrTmp = .Code.Text
      l = l + Len(StrTmp) - Len(Replace(StrTmp, "\n", "\"))
    End If
  End With
Next
MsgBox "There are " & l & " words in citations in this document.", , "Citation Word Count"
End Sub

1 个答案:

答案 0 :(得分:-1)

Paul Edstein(macropod)几个月前在http://www.msofficeforums.com/word-vba/33866-count-words-between-quotation-marks.html上写了一个关于这个问题的解决方案

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, j As Long
With ActiveDocument
  j = .ComputeStatistics(wdStatisticWords)
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[“" & Chr(34) & "]*[" & Chr(34) & "”]"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchWildcards = True
      .Execute
    End With
    Do While .Find.Found
      i = i + .ComputeStatistics(wdStatisticWords)
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
    MsgBox "This document contains " & j & " words ," & vbCr & _
      "of which " & i & " (" & Format(i * 100 / j, "0.00") & _
      "%) are in quotes."
  End With
End With
Application.ScreenUpdating = True
End Sub

这将为您提供总字数和引号中的字总数。要获得总数,只需从总单词中减去引号中的单词。