Link to home
Start Free TrialLog in
Avatar of YouGov2008
YouGov2008Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Counting words with particular formatting

Hi all,

I'm working with large Word documents which consist of text with a variety of formatting, including colours and font-weight. I'm writing a Macro which counts and displays the number of times particular words appear in this document, but I want to only count those occurrences which have particular formatting.

For example, I want to count the number of times the string "if" appears in a document where it is (a) a whole word, (b) in msBlue colour, (c) either begins a line or has only spaces before it, and (d) non-bold. Another example is that I want to find the string "[ADHOC]" where it is (a) a whole word, (b) in msBlue (c) is on a line of its own, and (d) bold. Occurrences of these words with any other formatting should not be counted.

Below is the code I have written so far for the "if" example. This counts the number of occurrences of the word "if" and displays it on the screen.

Any help with this would be appreciated.
Dim ifs As Integer
With ActiveDocument.Content.Find
    Do While .Execute(FindText:="if", Forward:=True, Format:=True, _
       MatchWholeWord:=True) = True
              
       ifs = ifs + 1
    Loop
End With
 
Response = MsgBox("'if' was found" _
& Str$(ifs) & " times.", vbOKOnly)

Open in new window

Avatar of Mike McCracken
Mike McCracken

I don't find anyway to add the formatting restrictions to the find command.

mlmcc
Add the format conditions for the Find as shown below. To manage the other conditions, you'll need to do more analysis for each found word.
With ActiveDocument.Content.Find
    .Font.Color = wdColorBlue
    Do While .Execute(FindText:="if", Forward:=True, Format:=True, _
       MatchWholeWord:=True) = True
'-- analyze the found content for your other conditions here and only increment the count if it passes
       ifs = ifs + 1
    Loop
End With

Open in new window

Avatar of YouGov2008

ASKER

Thanks Eric. That helps a bit. From what you've provided, I've added an additional line to also specify the bold/non-bold formatting as well, as seen in my latest version below. But it's the ability to count them only if they start a line (ignoring any tabs or spaces) in the case of the "if" string, or only counting them if they have a line to themselves (ignoring any tabs or spaces) in the case of the "[ADHOC]" string that I'm struggling with.

I don't feel I can allocate any points yet because this is definitely the hardest part of the task and I'm still stumped. Thanks for the help so far though.

Dim ifs As Integer
With ActiveDocument.Content.Find
    .Font.Color = wdColorBlue
    .Font.Bold = False
    Do While .Execute(FindText:="if", Forward:=True, Format:=True, _
       MatchWholeWord:=True) = True
                       
       ifs = ifs + 1
    Loop
End With

Open in new window

Hmm... well, you will need to add code to test for the other conditions before you increment the counter. The trick will be to figure out how to determine if a found item is on a line by itself, or is prefixed by "n" number of allowable characters (spaces, tabs, etc.) Perhaps a Select Case construct could do it.

If you tested the character before the found item ("if"), and it was a paragraph end or a new line character, you would know it started on a new line. If it was a space (or a tab?), you could continue to test for the next previous character, skipping any allowable characters until you came up with a non-allowed character or a paragraph or new line character.

I'm a bit fuzzy about how to manage this with Ranges, so I wasn't able to test it out myself.
I tried using the ^w to look for white space but it required a blank at the front or rear.

^p searches for paragraph marks.

Not sure if you can do that in code using the WORD search engine.

mlmcc
ASKER CERTIFIED SOLUTION
Avatar of Eric Fletcher
Eric Fletcher
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Eric,

By coincidence, I've been working with Wildcards since posting my original request for help and I think that they might hold the key to the solution because you can incorporate wildcard searches into macros. As you assumed, I do indeed want a code solution because this needs to be run by people with minimal knowledge of such things.

I thought that maybe I could first run a script that replaced any occurrences of [ ][ADHOC] with [ADHOC] i.e. that made sure that [ADHOC] was always at the start of a line. The code I used for this is below...

Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
        .Text = "[ ]@(\[ADHOC\])"
        .Replacement.Text = "[ADHOC]"
        .Font.Bold = True
        .Font.Color = wdColorBlue
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With

This works, to an extent, but it is very frustrating because once you specify that you want to use Wildcards then the search string is automatically case-sensitive (you can't turn this off), and there is no way of me knowing the case of what has been written. For example, I am just as likely to find [Adhoc] or [AdHoc] or [adhoc] or any other combination, and I need to count all of them.

I could write something which converted all cases of [adhoc] to [ADHOC] but I would prefer this to be non-interventionary. I.e. All I want to do is count things, not change anything.

The other frustration with Word Wildcards is that, unlike proper RegEx, there is no syntax for "0 or more cases". {0,} is not accepted syntax for some reason. Looks like an oversight on the part of the program designers. Anyway, this is a problem because I want to be able to count occurrences that have "0 or more spaces before them". I guess I could count the number of [ADHOC] tags with no spaces and count the number with 0 or more spaces and then add them together, but it's annoying anyway.
No workable solution provided, though perhaps the task has no solution.