2017-10-12 58 views
-1

我正在为我的大学撰写我的论文,而且我不允许将单词用在引号中,用于我的总字数。由于Word没有这样做的功能,我希望有人能够通过创建一个宏来帮助我。我之前使用过宏,但对于像这样复杂的事情,我的经验非常少(如果它甚至那么复杂)。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

[为什么“有人可以帮我吗?”不是一个实际的问题?](http://meta.stackoverflow.com/q/284236) –

+0

你用于引用的代码在哪里? – Robby

+0

对不起,现在添加引用文字的代码! –

回答

-1

保罗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 

这会给你总字数和引号中的单词总量。要获得总数,只需从总词中扣除引号中的单词。

+0

谢谢,当我看时,我找不到任何东西。显然没有看起来够硬 –

+1

@ Dom.Ibbz对于任何与Word VBA相关的东西,总是要经常去检查ms office论坛 - 那里有一些真正的Word MVP,那里面写了几乎所有东西以及Word在太阳下的所有东西(它们是如果你给他们时间,也非常愿意为你写免费代码)。 – dwirony

+0

谢谢您的帮助,我一定会在将来查看! –