Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Word count macro in Visual Basic for MS Word
New Posts  All Forums:Forum Nav:

Word count macro in Visual Basic for MS Word

post #1 of 8
Thread Starter 
Hey...if this is the wrong section, mods feel free to move it.

I'm trying to create a macro that enables a word count to be ran in MS Word. It's for school.

I came up with something but it isn't working with larger documents.
It's just plain inaccurate. When 400 words was tried, it read 700.

It works with smaller documents (1 paragraph or so) thanks to the - 1 next to the word count.
I have no clue why. Any help would be greatly appreciated.

Here is the code I have:

Code:
Sub Word_Count()
WordCount = ActiveDocument.words.Count
MsgBox ("Number of words in this document is:" & Space(1) & (WordCount - 1))
End Sub

Edited by vinton13 - 6/10/13 at 10:33am
Apollo
(16 items)
 
Project Greyscale
(24 items)
 
Zeus
(16 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7-5930K Asus Sabertooth X99 ASUS GeForce GTX 1080 8GB ROG STRIX  Corsair Vengeance 32GB (4x8GB) DDR4 3000 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 500GB Samsung 850 EVO 1TB Samsung SH-224BB CM Hyper 212 EVO 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium (Cherry Red) Corsair AX850 
CaseMouseMouse PadAudio
Cooler Master HAF X Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC1150 8-Channel + Logitech z906 + Sam... 
CPUMotherboardGraphicsGraphics
i7-4790K EVGA Z97 Classified EVGA GTX 780Ti EVGA GTX 780Ti 
RAMHard DriveHard DriveHard Drive
Dominator Platinum Spinpoint F3 Spinpoint F3 510 Series 
Hard DriveHard DriveCoolingCooling
510 Series 320 Series XSPC AX120 XSPC AX360 
CoolingCoolingCoolingOS
XSPC AX240 EK Supremacy EK FC780 GTX Ti WIndows 8.1 
OSMonitorPowerCase
OS X 10.9.3 Yamaski Catleap 120Hz HCP-1200 Air 540 
MouseMouse PadAudioAudio
Razer Deathadder Table Avid Fast Track Duo HiFIMan HE-400 
CPUMotherboardGraphicsRAM
AMD FX-8120 @4.5GHz GIGABYTE GA-990FXA-UD7 GIGABYTE Radeon RX 460 G.SKILL Sniper 1600MHz 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial 64GB m4 1TB WD Black Samsung SH-224BB CM Hyper 212+ 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium Corsair AX850 
CaseMouseMouse PadAudio
CM 690II Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC889 + Logitech z906 + Samson SR850 
  hide details  
Reply
Apollo
(16 items)
 
Project Greyscale
(24 items)
 
Zeus
(16 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7-5930K Asus Sabertooth X99 ASUS GeForce GTX 1080 8GB ROG STRIX  Corsair Vengeance 32GB (4x8GB) DDR4 3000 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 500GB Samsung 850 EVO 1TB Samsung SH-224BB CM Hyper 212 EVO 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium (Cherry Red) Corsair AX850 
CaseMouseMouse PadAudio
Cooler Master HAF X Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC1150 8-Channel + Logitech z906 + Sam... 
CPUMotherboardGraphicsGraphics
i7-4790K EVGA Z97 Classified EVGA GTX 780Ti EVGA GTX 780Ti 
RAMHard DriveHard DriveHard Drive
Dominator Platinum Spinpoint F3 Spinpoint F3 510 Series 
Hard DriveHard DriveCoolingCooling
510 Series 320 Series XSPC AX120 XSPC AX360 
CoolingCoolingCoolingOS
XSPC AX240 EK Supremacy EK FC780 GTX Ti WIndows 8.1 
OSMonitorPowerCase
OS X 10.9.3 Yamaski Catleap 120Hz HCP-1200 Air 540 
MouseMouse PadAudioAudio
Razer Deathadder Table Avid Fast Track Duo HiFIMan HE-400 
CPUMotherboardGraphicsRAM
AMD FX-8120 @4.5GHz GIGABYTE GA-990FXA-UD7 GIGABYTE Radeon RX 460 G.SKILL Sniper 1600MHz 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial 64GB m4 1TB WD Black Samsung SH-224BB CM Hyper 212+ 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium Corsair AX850 
CaseMouseMouse PadAudio
CM 690II Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC889 + Logitech z906 + Samson SR850 
  hide details  
Reply
post #2 of 8
Quote:
The Words property returns a collection that represents all the words in a range, selection, or document. The collection includes all punctuation and paragraph marks in the selection or the document.

For example, if you attempt to determine the word count in the following three lines of text
This is x number of words.
This is x number of words.
This is x number of words.
by using the Words property as follows
Sub GetWords()

MsgBox ActiveDocument.Words.Count

End Sub

the result is 24 words.

To return only the number of words in a document or a range, excluding paragraph marks and punctuation, use the ComputeStatistics method instead of the Words property.

The ComputeStatistics method returns a word count that does not include paragraph marks or punctuation. By using the sample text from the "Cause" section, the following ComputeStatistics method code returns a count of 18 words:

Sub GetWords()

MsgBox ActiveDocument.Range.ComputeStatistics(wdStatisticWords)

End Sub
LikeCorsairMuch
(29 items)
 
  
CPUMotherboardGraphicsRAM
intel i7 2600k Asus p8Z68 V LE Asus GTX 670 Corsair Vengeance 16GB 1600 
Hard DriveHard DriveHard DriveHard Drive
Corsair Force 3 120GB SSD Corsair Force 3 GT 240GB SSD (Red) WD 2TB Caviar Black SeaGate 500GB HDD 
Optical DriveOptical DriveCoolingCooling
Asus DVD-RW LG Blu-Ray RW w/ lightscribe Corsair H100 Aero cool Shark Fan 120mm 
CoolingCoolingCoolingCooling
Aero cool Shark Fan 120mm Aero cool Shark Fan 120mm Aero cool Shark Fan 120mm Aero cool Shark Fan 140mm 
CoolingCoolingOSMonitor
Aero cool Shark Fan 140mm Aero cool Shark Fan 140mm Windows 7 Ultimate 64bit Asus  
MonitorMonitorKeyboardPower
Asus Asus Corsair k90 Mechanical Corsair AX1200W 
CaseMouseMouse PadAudio
Corsair 800D Razer Naga Smiley Face Sony optical HTS 
Audio
Corsair 2500 Wireless Headset 
  hide details  
Reply
LikeCorsairMuch
(29 items)
 
  
CPUMotherboardGraphicsRAM
intel i7 2600k Asus p8Z68 V LE Asus GTX 670 Corsair Vengeance 16GB 1600 
Hard DriveHard DriveHard DriveHard Drive
Corsair Force 3 120GB SSD Corsair Force 3 GT 240GB SSD (Red) WD 2TB Caviar Black SeaGate 500GB HDD 
Optical DriveOptical DriveCoolingCooling
Asus DVD-RW LG Blu-Ray RW w/ lightscribe Corsair H100 Aero cool Shark Fan 120mm 
CoolingCoolingCoolingCooling
Aero cool Shark Fan 120mm Aero cool Shark Fan 120mm Aero cool Shark Fan 120mm Aero cool Shark Fan 140mm 
CoolingCoolingOSMonitor
Aero cool Shark Fan 140mm Aero cool Shark Fan 140mm Windows 7 Ultimate 64bit Asus  
MonitorMonitorKeyboardPower
Asus Asus Corsair k90 Mechanical Corsair AX1200W 
CaseMouseMouse PadAudio
Corsair 800D Razer Naga Smiley Face Sony optical HTS 
Audio
Corsair 2500 Wireless Headset 
  hide details  
Reply
post #3 of 8
Thread Starter 
Omg thank you so much! +Rep to you good sir!
Apollo
(16 items)
 
Project Greyscale
(24 items)
 
Zeus
(16 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7-5930K Asus Sabertooth X99 ASUS GeForce GTX 1080 8GB ROG STRIX  Corsair Vengeance 32GB (4x8GB) DDR4 3000 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 500GB Samsung 850 EVO 1TB Samsung SH-224BB CM Hyper 212 EVO 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium (Cherry Red) Corsair AX850 
CaseMouseMouse PadAudio
Cooler Master HAF X Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC1150 8-Channel + Logitech z906 + Sam... 
CPUMotherboardGraphicsGraphics
i7-4790K EVGA Z97 Classified EVGA GTX 780Ti EVGA GTX 780Ti 
RAMHard DriveHard DriveHard Drive
Dominator Platinum Spinpoint F3 Spinpoint F3 510 Series 
Hard DriveHard DriveCoolingCooling
510 Series 320 Series XSPC AX120 XSPC AX360 
CoolingCoolingCoolingOS
XSPC AX240 EK Supremacy EK FC780 GTX Ti WIndows 8.1 
OSMonitorPowerCase
OS X 10.9.3 Yamaski Catleap 120Hz HCP-1200 Air 540 
MouseMouse PadAudioAudio
Razer Deathadder Table Avid Fast Track Duo HiFIMan HE-400 
CPUMotherboardGraphicsRAM
AMD FX-8120 @4.5GHz GIGABYTE GA-990FXA-UD7 GIGABYTE Radeon RX 460 G.SKILL Sniper 1600MHz 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial 64GB m4 1TB WD Black Samsung SH-224BB CM Hyper 212+ 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium Corsair AX850 
CaseMouseMouse PadAudio
CM 690II Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC889 + Logitech z906 + Samson SR850 
  hide details  
Reply
Apollo
(16 items)
 
Project Greyscale
(24 items)
 
Zeus
(16 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7-5930K Asus Sabertooth X99 ASUS GeForce GTX 1080 8GB ROG STRIX  Corsair Vengeance 32GB (4x8GB) DDR4 3000 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 500GB Samsung 850 EVO 1TB Samsung SH-224BB CM Hyper 212 EVO 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium (Cherry Red) Corsair AX850 
CaseMouseMouse PadAudio
Cooler Master HAF X Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC1150 8-Channel + Logitech z906 + Sam... 
CPUMotherboardGraphicsGraphics
i7-4790K EVGA Z97 Classified EVGA GTX 780Ti EVGA GTX 780Ti 
RAMHard DriveHard DriveHard Drive
Dominator Platinum Spinpoint F3 Spinpoint F3 510 Series 
Hard DriveHard DriveCoolingCooling
510 Series 320 Series XSPC AX120 XSPC AX360 
CoolingCoolingCoolingOS
XSPC AX240 EK Supremacy EK FC780 GTX Ti WIndows 8.1 
OSMonitorPowerCase
OS X 10.9.3 Yamaski Catleap 120Hz HCP-1200 Air 540 
MouseMouse PadAudioAudio
Razer Deathadder Table Avid Fast Track Duo HiFIMan HE-400 
CPUMotherboardGraphicsRAM
AMD FX-8120 @4.5GHz GIGABYTE GA-990FXA-UD7 GIGABYTE Radeon RX 460 G.SKILL Sniper 1600MHz 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial 64GB m4 1TB WD Black Samsung SH-224BB CM Hyper 212+ 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium Corsair AX850 
CaseMouseMouse PadAudio
CM 690II Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC889 + Logitech z906 + Samson SR850 
  hide details  
Reply
post #4 of 8
I'm glad OP got his answer, but isn't there a word count function in MS Word already builtin?

Unless I missed the purpose of the exercise and OP needs to read the file from cli and count words in files in batch - nevermind OP says "run in word"....

So:


AMD Rig
(12 items)
 
  
CPUMotherboardGraphicsGraphics
8320 FX Gigabyte 990FX-UD3 XFX R9 270X XFX R9 270X 
RAMHard DriveOptical DriveCooling
Crucial Ballistix Sport Kingston V300 Bluray Cooler Master Seidon 240M 
OSKeyboardPowerCase
Windows 7 x64 Ultimate CM QuickFire Corsair CX750M Corsair 540 Air 
  hide details  
Reply
AMD Rig
(12 items)
 
  
CPUMotherboardGraphicsGraphics
8320 FX Gigabyte 990FX-UD3 XFX R9 270X XFX R9 270X 
RAMHard DriveOptical DriveCooling
Crucial Ballistix Sport Kingston V300 Bluray Cooler Master Seidon 240M 
OSKeyboardPowerCase
Windows 7 x64 Ultimate CM QuickFire Corsair CX750M Corsair 540 Air 
  hide details  
Reply
post #5 of 8
Thread Starter 
I know there is, but it's a school exercise, we're now breaking into programming.
No clue why they wanted us to do this particular one though. confused.gif
Edited by vinton13 - 6/10/13 at 3:23pm
Apollo
(16 items)
 
Project Greyscale
(24 items)
 
Zeus
(16 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7-5930K Asus Sabertooth X99 ASUS GeForce GTX 1080 8GB ROG STRIX  Corsair Vengeance 32GB (4x8GB) DDR4 3000 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 500GB Samsung 850 EVO 1TB Samsung SH-224BB CM Hyper 212 EVO 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium (Cherry Red) Corsair AX850 
CaseMouseMouse PadAudio
Cooler Master HAF X Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC1150 8-Channel + Logitech z906 + Sam... 
CPUMotherboardGraphicsGraphics
i7-4790K EVGA Z97 Classified EVGA GTX 780Ti EVGA GTX 780Ti 
RAMHard DriveHard DriveHard Drive
Dominator Platinum Spinpoint F3 Spinpoint F3 510 Series 
Hard DriveHard DriveCoolingCooling
510 Series 320 Series XSPC AX120 XSPC AX360 
CoolingCoolingCoolingOS
XSPC AX240 EK Supremacy EK FC780 GTX Ti WIndows 8.1 
OSMonitorPowerCase
OS X 10.9.3 Yamaski Catleap 120Hz HCP-1200 Air 540 
MouseMouse PadAudioAudio
Razer Deathadder Table Avid Fast Track Duo HiFIMan HE-400 
CPUMotherboardGraphicsRAM
AMD FX-8120 @4.5GHz GIGABYTE GA-990FXA-UD7 GIGABYTE Radeon RX 460 G.SKILL Sniper 1600MHz 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial 64GB m4 1TB WD Black Samsung SH-224BB CM Hyper 212+ 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium Corsair AX850 
CaseMouseMouse PadAudio
CM 690II Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC889 + Logitech z906 + Samson SR850 
  hide details  
Reply
Apollo
(16 items)
 
Project Greyscale
(24 items)
 
Zeus
(16 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7-5930K Asus Sabertooth X99 ASUS GeForce GTX 1080 8GB ROG STRIX  Corsair Vengeance 32GB (4x8GB) DDR4 3000 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 500GB Samsung 850 EVO 1TB Samsung SH-224BB CM Hyper 212 EVO 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium (Cherry Red) Corsair AX850 
CaseMouseMouse PadAudio
Cooler Master HAF X Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC1150 8-Channel + Logitech z906 + Sam... 
CPUMotherboardGraphicsGraphics
i7-4790K EVGA Z97 Classified EVGA GTX 780Ti EVGA GTX 780Ti 
RAMHard DriveHard DriveHard Drive
Dominator Platinum Spinpoint F3 Spinpoint F3 510 Series 
Hard DriveHard DriveCoolingCooling
510 Series 320 Series XSPC AX120 XSPC AX360 
CoolingCoolingCoolingOS
XSPC AX240 EK Supremacy EK FC780 GTX Ti WIndows 8.1 
OSMonitorPowerCase
OS X 10.9.3 Yamaski Catleap 120Hz HCP-1200 Air 540 
MouseMouse PadAudioAudio
Razer Deathadder Table Avid Fast Track Duo HiFIMan HE-400 
CPUMotherboardGraphicsRAM
AMD FX-8120 @4.5GHz GIGABYTE GA-990FXA-UD7 GIGABYTE Radeon RX 460 G.SKILL Sniper 1600MHz 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial 64GB m4 1TB WD Black Samsung SH-224BB CM Hyper 212+ 
OSMonitorKeyboardPower
Windows 8.1 x64 Pro Asus PB278Q (1440p) Gigabyte Aivia Osmium Corsair AX850 
CaseMouseMouse PadAudio
CM 690II Gigabyte Aivia Krypton Razer Goliathus Extended Speed Edition Realtek ALC889 + Logitech z906 + Samson SR850 
  hide details  
Reply
post #6 of 8
Ah ok makes sense, hope I didn't come off in a bad way - I'm just used to dealing with people duplicating existing crap all day long frown.gif
AMD Rig
(12 items)
 
  
CPUMotherboardGraphicsGraphics
8320 FX Gigabyte 990FX-UD3 XFX R9 270X XFX R9 270X 
RAMHard DriveOptical DriveCooling
Crucial Ballistix Sport Kingston V300 Bluray Cooler Master Seidon 240M 
OSKeyboardPowerCase
Windows 7 x64 Ultimate CM QuickFire Corsair CX750M Corsair 540 Air 
  hide details  
Reply
AMD Rig
(12 items)
 
  
CPUMotherboardGraphicsGraphics
8320 FX Gigabyte 990FX-UD3 XFX R9 270X XFX R9 270X 
RAMHard DriveOptical DriveCooling
Crucial Ballistix Sport Kingston V300 Bluray Cooler Master Seidon 240M 
OSKeyboardPowerCase
Windows 7 x64 Ultimate CM QuickFire Corsair CX750M Corsair 540 Air 
  hide details  
Reply
post #7 of 8
Quote:
Originally Posted by vinton13 View Post

Omg thank you so much! +Rep to you good sir!
No problem, glad I could help.
LikeCorsairMuch
(29 items)
 
  
CPUMotherboardGraphicsRAM
intel i7 2600k Asus p8Z68 V LE Asus GTX 670 Corsair Vengeance 16GB 1600 
Hard DriveHard DriveHard DriveHard Drive
Corsair Force 3 120GB SSD Corsair Force 3 GT 240GB SSD (Red) WD 2TB Caviar Black SeaGate 500GB HDD 
Optical DriveOptical DriveCoolingCooling
Asus DVD-RW LG Blu-Ray RW w/ lightscribe Corsair H100 Aero cool Shark Fan 120mm 
CoolingCoolingCoolingCooling
Aero cool Shark Fan 120mm Aero cool Shark Fan 120mm Aero cool Shark Fan 120mm Aero cool Shark Fan 140mm 
CoolingCoolingOSMonitor
Aero cool Shark Fan 140mm Aero cool Shark Fan 140mm Windows 7 Ultimate 64bit Asus  
MonitorMonitorKeyboardPower
Asus Asus Corsair k90 Mechanical Corsair AX1200W 
CaseMouseMouse PadAudio
Corsair 800D Razer Naga Smiley Face Sony optical HTS 
Audio
Corsair 2500 Wireless Headset 
  hide details  
Reply
LikeCorsairMuch
(29 items)
 
  
CPUMotherboardGraphicsRAM
intel i7 2600k Asus p8Z68 V LE Asus GTX 670 Corsair Vengeance 16GB 1600 
Hard DriveHard DriveHard DriveHard Drive
Corsair Force 3 120GB SSD Corsair Force 3 GT 240GB SSD (Red) WD 2TB Caviar Black SeaGate 500GB HDD 
Optical DriveOptical DriveCoolingCooling
Asus DVD-RW LG Blu-Ray RW w/ lightscribe Corsair H100 Aero cool Shark Fan 120mm 
CoolingCoolingCoolingCooling
Aero cool Shark Fan 120mm Aero cool Shark Fan 120mm Aero cool Shark Fan 120mm Aero cool Shark Fan 140mm 
CoolingCoolingOSMonitor
Aero cool Shark Fan 140mm Aero cool Shark Fan 140mm Windows 7 Ultimate 64bit Asus  
MonitorMonitorKeyboardPower
Asus Asus Corsair k90 Mechanical Corsair AX1200W 
CaseMouseMouse PadAudio
Corsair 800D Razer Naga Smiley Face Sony optical HTS 
Audio
Corsair 2500 Wireless Headset 
  hide details  
Reply
post #8 of 8
Quote:
Originally Posted by xyexz View Post

I'm glad OP got his answer, but isn't there a word count function in MS Word already builtin?

Unless I missed the purpose of the exercise and OP needs to read the file from cli and count words in files in batch - nevermind OP says "run in word"....

So:



There is, and all that the solution given does is call that function by API.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Word count macro in Visual Basic for MS Word