-1

I have a word template that has a variable number of occurrences of the "SEQ" field "testnum" tag (depending on how much information needs to be included - minumum 1 SEQ field, no maximum.) I would like to create a section at the top that calculates how often SEQ appears in the document, and displays a total. To make the template easy to maintain for others, I would like to use the F9 function key to enter the solution, instead of having it in vba.

Ideally, it would be possible to count by the tag occurrence, without creating additional bookmarks, but I am opent to any solutions. Thank you!

1 Answers1

0

When you record a Macro in Word you can press the Keyboard button to assign a keyboard shortcut to it. I wouldn't use F9 though as it is extremely useful. (When you press the keyboard-combination Word will show which command, if any, it is currently assigned to.)

Here is some code you can study/explore:

Sub CheckSEQs()
    Dim fld As Field
    Dim iTotal As Integer

    iTotal = 0
    For Each fld In ActiveDocument.Fields
        'Debug.Print fld.Code
        If InStr(fld.Code, "SEQ") > 0 Then
            'search for "testnum" as well
            iTotal = iTotal + 1
        End If
    Next fld
    MsgBox "There are " & iTotal & " SEQ fields."
End Sub
Andy G
  • 19,232
  • 5
  • 47
  • 69