Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-26-2022, 10:52 AM
laith93 laith93 is offline Macro to insert certain words if the number of words than 20 Windows 10 Macro to insert certain words if the number of words than 20 Office 2019
Competent Performer
Macro to insert certain words if the number of words than 20
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Post Macro to insert certain words if the number of words than 20

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
Reply With Quote
  #2  
Old 10-26-2022, 02:54 PM
Guessed's Avatar
Guessed Guessed is offline Macro to insert certain words if the number of words than 20 Windows 10 Macro to insert certain words if the number of words than 20 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 10-27-2022, 08:04 AM
laith93 laith93 is offline Macro to insert certain words if the number of words than 20 Windows 10 Macro to insert certain words if the number of words than 20 Office 2019
Competent Performer
Macro to insert certain words if the number of words than 20
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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.
Thanks, Mr. Andrew,
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.
Reply With Quote
  #4  
Old 10-27-2022, 03:47 PM
Guessed's Avatar
Guessed Guessed is offline Macro to insert certain words if the number of words than 20 Windows 10 Macro to insert certain words if the number of words than 20 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 10-28-2022, 12:02 AM
laith93 laith93 is offline Macro to insert certain words if the number of words than 20 Windows 10 Macro to insert certain words if the number of words than 20 Office 2019
Competent Performer
Macro to insert certain words if the number of words than 20
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Try this code
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 .
Attached Files
File Type: docx Title.docx (18.4 KB, 5 views)
Reply With Quote
  #6  
Old 10-28-2022, 01:01 AM
Guessed's Avatar
Guessed Guessed is offline Macro to insert certain words if the number of words than 20 Windows 10 Macro to insert certain words if the number of words than 20 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 10-28-2022, 01:12 AM
laith93 laith93 is offline Macro to insert certain words if the number of words than 20 Windows 10 Macro to insert certain words if the number of words than 20 Office 2019
Competent Performer
Macro to insert certain words if the number of words than 20
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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.
Thank you, thank you, thank you, dear Andrew
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
Reply With Quote
Reply

Tags
vba code, word vba code, word vba macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to insert certain words if the number of words than 20 Insert words before and after any italics 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
Macro to insert certain words if the number of words than 20 How to mark underlined words in a sentence as A, B, C, D (beneath the words) thudangky Word 13 12-12-2013 02:22 AM
Macro to insert certain words if the number of words than 20 Macro for highlighting specific number of words 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:05 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft