Column: Word Count for Microsoft Word documents

Install this script add-in to gain a Word Count column which will display the number of words in .doc and .docx files.

To Install:

  • Download: DocWordCount.js.txt (2.16 KB)
  • Drag the .js.txt file to Preferences / Toolbars / Scripts.
  • Add the Scripts > Word Count column to your file display.

Requirements:

  • You need to have Microsoft Word installed.
  • So far only tested with Word 2010.

Notes / Warnings:

  • The script talks to Microsoft Word to get statistics about the document. To do this, it has to ask Word to open the file, and then tells Word to close it when done.
  • As a result, if you have Word open, you may see its windows flicker as files are loaded and unloaded.
  • The script tries to detect if a document is already loaded into Word, and uses the existing open document if so, without closing it when done. There is a chance that detection won't always work, which might result in you being prompted to close the file you're editing (or having it close itself if there are no unsaved changes). If you notice anything strange happening within Word, close the Opus windows that have the column in them, and please report the problem here so the script might be improved.

Script code:

You do not need the code below -- a copy of it is in the download, above -- but it is here for people browsing the forum looking for scripting techniques:

// DocWordCount
// 
// This is a script for Directory Opus.
// See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.

// Called by Directory Opus to initialize the script
function OnInit(initData)
{
	initData.name = "Doc Word Count";
	initData.desc = "Word count column for Microsoft Word documents. (Requires Microsoft Word.)";
	initData.copyright = "(C) 2015 Leo Davidson";
	initData.version = "1.0";
	initData.default_enable = true;

	var col = initData.AddColumn();
	col.name = "DocWordCount";
	col.method = "OnDocWordCount";
	col.label = "Word Count";
	col.justify = "left";
	col.autogroup = true;
	col.autorefresh = true;
	col.type = "number";
}

var fsu = null;
var wordApp = null;

// Implement the DocWordCount column
function OnDocWordCount(scriptColData)
{
	var fileExt = scriptColData.item.ext;

	if (scriptColData.col != "DocWordCount"
	||  (fileExt != ".doc" && fileExt != ".docx")
	||  scriptColData.item.name[0] == "~")
	{
		return;
	}

	if (fsu == null)
	{
		fsu = DOpus.FSUtil;
	}

	if (wordApp == null)
	{
		try
		{
			wordApp = GetObject("", "Word.Application");
		}
		catch(e)
		{
			wordApp = null;
		}
		if (wordApp == null)
		{
			wordApp = new ActiveXObject("Word.Application");
		}
		if (wordApp == null)
		{
			DOpus.Output("Failed to open Word", true);
			return;
		}

		// wordApp.Visible = true;
	}

	var wordDocuments = wordApp.Documents;
	var filePath = scriptColData.item.realpath + "";
	var wordFile = null;
	var closeFile = false;
	
	for(var docEnum = new Enumerator(wordDocuments); !docEnum.atEnd(); docEnum.moveNext())
	{
		var fullPath = docEnum.item().Path + "\\" + docEnum.item().Name;
		if (fsu.ComparePath(fullPath, filePath))
		{
			DOpus.Output("File already loaded: " + fullPath);
			wordFile = docEnum.item();
			break;
		}
	}

	if (wordFile == null)
	{
		wordFile = wordDocuments.open(filePath, false, true, false);
		closeFile = true;
		DOpus.Output("Loaded: " + filePath);
	}

	if (wordFile != null)
	{
		scriptColData.value = wordFile.ReadabilityStatistics("words").Value;
		
		if (closeFile)
		{
			wordFile.Close();
		}
	}
}
1 Like

Works perfectly with Word 2013...nice script.

Hi Leo,

Sorry I hadn't noticed this. Many thanks for the add-in, except I only get error messages, such as "ToolsGrammarStatisticsArray cannot be run on a document that contains more than one language format"

and

"Doc Word Count: Error at line 93, position 3
Doc Word Count: The requested member of the collection does not exist. (0x800a1735)
Doc Word Count: File already loaded: C:\Users\Olly\SkyDrive\Binnen\2015\juni\83689 Pokon BIO Groente en Kruiden pillen (origineel).docx
Doc Word Count: Error at line 93, position 3
Doc Word Count: The requested member of the collection does not exist. (0x800a1735)"

I have Word 2010. Any ideas?

Thanks,

Olly

I'm not an MS Word expert but from the first error message I guess that Word won't provide statistics (even simple word counts) from mixed-language documents. I don't know if there is any way around that problem for such documents.

The "file already loaded" lines can be ignored. That was just a diagnostic message which I think I forgot to remove from the script, but is harmless.

Hi Leo,

I had to disable the add-in because all docs were now opening in "compatibility mode" (i.e. non-editable), wherever I opened them from. Could this be why I wasn't getting word counts? The word count in Word itself has no problem with more than one language format...

Thanks,

Olly

I don't know why that would happen, sorry. Not sure why Word would do that.

If I'm correct and this line fails for Olly:
scriptColData.value = wordFile.ReadabilityStatistics("words").Value;

The script will abort and the following:
wordFile.Close();

Won't be called while leaving the file in an "opened" status, making it "readonly"?
But.. just an assumption.

Hello, can you elaborate on the installation process, in particular where this occurs:
"Add the Scripts > Word Count column to your file display."
Thank you.

Columns can be added by right-clicking the column header:


Or via the Folder Options dialog:


You can save the columns for a particular folder, or as the new default, via Folder Options.

1 Like

Hello, is there a chance this script may invoke MS Word opening documents on startup?

Recently on startup Word opens with all sort of documents, that may be on a tab with file collections or not.
I have tried everything non D.Opus related, I found o the web, but nothing seems to work.

The quickest way to find out is to turn off the column in the preferences.

I did exactly that and on reboot, no Ms Word instances open at. So, problem, solved, but lost word count in Opus.

The script doesn't ask Word to open any windows so if that's happening it's a bug in Word. (Doesn't surprise me as Office is very badly written, even today.)

1 Like

thank you!

Is there an easy way to adapt this to report markdown word count as well?

Not unless there's a way for Word to load .md files. (Maybe there is?) Although a basic text word count (treating the markup as normal words) would probably be quite close to correct for typical markdown text, I think?

I wrote a small add-in that works with markdown files.

1 Like