![]() |
#1
|
|||
|
|||
![]()
Hi,
I have a template word file for checking, the checking system ensures me to insert at least 20 words, sometimes the number of words is less than 20, so it requires additional words in order to be 20 in total. However, I want a macro that firstly counts the number of words, then if the number of words is less than 20, inserts certain additional words to be 20 words exactly, if it is 20 and more, does nothing. Thanks |
#2
|
||||
|
||||
![]()
We can't answer this without knowing how you are providing the 20 words. They might be in a string or an array or a range or a dictionary etc.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
![]() Quote:
Excuse me, It could be string, also it could be any 20 words for example (test, demo,test, demo,test, demo,test, demo,test, demo,test, demo,test, demo,test, demo,test, demo,test, demo). So for instance, if the number of words in the document is 3, insert 17 words from previously mentioned words, and in such a manner. |
#4
|
||||
|
||||
![]()
Try this code
Code:
Sub Macro1() Dim iWords As Double, sFiller As String, arrFiller() As String, i As Integer sFiller = "I didn't bother to attempt my homework by myself but I asked someone on the internet to do it for me" arrFiller = Split(sFiller, " ") iWords = ActiveDocument.Words.Count Debug.Print iWords If iWords < 20 Then For i = 0 To 20 - iWords ActiveDocument.Range.InsertAfter " " & arrFiller(i) Next i End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
![]()
Dear Andrew, Thanks so much
But, excuse me, dear Andrew, the code works fine and perfectly for some files but not for others (I have been shocked) See the attachment title file, although the words number is <20, it inserts additional words to be 14 in total (not 20) I have another file, it inserts additional words to be 16 in total (not 20) I don't know what is the problem with these files. So any suggestion with my special thanks ![]() |
#6
|
||||
|
||||
![]()
There are different ways to calculate a word count in Word. The way the earlier code determines word count is not what a normal English speaker would consider the Word count - it includes punctuation as a word. This code modification uses an alternate method to arrive at a word count and might work better for your documents
Code:
Sub Macro1() Dim iWords As Double, sFiller As String, arrFiller() As String, i As Integer sFiller = "I didn't bother to attempt my homework by myself but I asked someone on the internet to do it for me" arrFiller = Split(sFiller, " ") iWords = ActiveDocument.Range.ComputeStatistics(wdStatisticWords) 'ActiveDocument.Words.Count If iWords < 20 Then For i = 0 To 19 - iWords ActiveDocument.Range.InsertAfter " " & arrFiller(i) Next i End If End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
![]() Quote:
I can't thank you enough. The code now works fine and perfectly for all files ![]() Thank you for this new info for methods of counting word counts. Best Regards |
![]() |
Tags |
vba code, word vba code, word vba macro |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
benfarley | Word VBA | 1 | 03-30-2022 08:35 PM |
How to find (highlight) two and more words in a list of 75k single words in Word 2010 | Usora | Word | 8 | 05-29-2018 03:34 AM |
![]() |
thudangky | Word | 13 | 12-12-2013 02:22 AM |
![]() |
icsjohn | Word VBA | 2 | 12-07-2011 06:44 PM |
Why Words doesn’t show the style of the selected words automatically???? | Jamal NUMAN | Word | 0 | 04-14-2011 03:20 PM |