*新编码器*需要帮助计算以前搜索过的VBA WORD 2016宏的单词的出现(任何帮助将不胜感激:)) [英] * New Coder * Need help counting occurences of a previously searched for word for VBA WORD 2016 Macros (Any Help would be greatly appreciated :))
问题描述
我似乎无法获得这些要求的正确代码,我也应该
I can't seem to get the correct code for these requirements, I am supposed too
1)使用输入框提示用户输入搜索词
1) Prompt the user for a search word using an input box
2)使用ActiveDocument对象的功能计算该单词的实例数
2) Count the number of instances of that word using the features of ActiveDocument object
3)通过MsgBox显示实例的计数
3) Display the count of the instances via a MsgBox
4)使用选择将有关实例数(不区分大小写)的信息写入当前活动的word文档对象
4) Write information about the # of instances (not case sensitive) into the currently active word document using the selection object
推荐答案
以下代码改编自
https://wordribbon.tips.net/T010761_Generating_a_Count_of_Word_Occurrences
Sub FindWords()
Dim sWord As String
Dim lCount As Long
Dim sReturn As String
' Identify the word to count
sWord = InputBox(Prompt:="What word do you want to count?")
If sWord = "" Then Exit Sub
With ActiveDocument.Content.Find
.ClearFormatting ' ignore formatting
.Text = sWord ' the word to count
.MatchCase = False ' not case sensitive
.MatchWholeWord = True ' whole words only
' Loop and count each instance
Do While .Execute
lCount = lCount + 1
Loop
End With
sReturn = "The text '" & sWord & "' occurs " & lCount & " time(s)"
MsgBox sReturn, vbInformation
Selection.TypeText sReturn
End Sub
注意:代码只计算单词文档的正文部分,而不是页眉,页脚或文本框。
Note: the code will only count words in the body part of the document, not in headers, footers or text boxes.
这篇关于*新编码器*需要帮助计算以前搜索过的VBA WORD 2016宏的单词的出现(任何帮助将不胜感激:))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!