Word Add-in Word Count Excluding Tables & Headers

111 Views Asked by At

I'm VERY new to Word add-ins, but saw them in VS as (falsely) advertised as C# projects, and as someone who's dabbled in that for a while I thought I'd give it a go to save myself time instead of having to open up Notepad and doing the math by selecting various elements.

I then realised that pretty much all the functional code is in js, and we don't get along!

After trying and failing for the last day, I've decided to just ask.

My functional code for just trying to get the total wordcount is below, but even this doesn't seem to work.

function calcWordCount() {
        Word.run(function (context) {

            var range = context.document.body;

            var totalWC;

            context.load(range, 'text');

            var words = range.text.split(/\s+/);

            totalWC = words.length;

            context.load(totalWC, 'font');

            $("#fullWC").text(totalWC);

            return context.sync();
        })
        .catch(errorHandler);
    }

I have absolutely no idea what I'm doing with this, but did find this vb macro do what what I am looking to do here: https://answers.microsoft.com/en-us/msoffice/forum/all/excluding-words-from-the-word-count/e6f59014-66f2-4b43-83d2-df52f7b4da5b Problem is, due to security I can't run macros for my work, so I need to be able to do this within an add-in. Does anyone know what I'm doing wrong, and how I can: A) count the total words B) identify table and header word-count and minus that from the total

I'm completely lost here, the Office documentation for this seems to be VERY sparse.

0

There are 0 best solutions below