?
Solved

Tally content control results (Word 2010)

Posted on 2014-01-16
5
Medium Priority
?
520 Views
Last Modified: 2014-01-17
Hi Folks!  Been a long time since I was here..... wow.  Guess I'm still #2 overall in Word!  Grandkids take all my time now :)

In any event, I'm a bit rusty and need to count the number of content controls in a Word document that contain a certain phrase.  There are two items in the dropdown: "Inside authorized area" and "Outside authorized area"  

This document may be used on a daily basis, and after the individual has completed the entry for the day, I would like it to tally and give a message box saying how many days he has been "Outside authorized area".  I guess a DOCVARIABLE at the bottom and a MsgBox pop up.

I've attached a document which I started naming the content controls only up through January.  Hope this makes sense.  If you have a better idea, please offer it!  Many thanks.
Location-Tracking-Calendar.docx
0
Comment
Question by:Joanne M. Orzech
  • 3
  • 2
5 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 39786816
Hi Joanne and how are the grandchildren - and your son and his wife? I still have a picture from 2007 of one (the first?) of the babies.

Here is some code. It creates or updates a document variable so you can put one or more DOCVARIABLE fields somewhere on the document to display the result.
Sub CountCCbyText()
    Dim CC As ContentControl
    Dim iCount As Integer
    Dim strText As String
    Dim docVar As Variable
    
    strText = "Outside authorized area"
    For Each CC In ActiveDocument.ContentControls
        If CC.Range.Text = strText Then
            iCount = iCount + 1
        End If
    Next CC
    
    For Each docVar In ActiveDocument.Variables
        If docVar.Name = "varOutSide" Then
            docVar.Value = iCount
            Exit Sub
        End If
    Next docVar
    
    If docVar Is Nothing Then
        Set docVar = ActiveDocument.Variables.Add("varOutSide", iCount)
    End If
    
    MsgBox "There are " & iCount & " instances of " & """" & strText & """"
End Sub

Open in new window

0
 
LVL 37

Author Comment

by:Joanne M. Orzech
ID: 39788334
Everyone is great Graham!  Thank you for asking.  My oldest granddaughter is going to be 8 and Anthony will be 3!  Such a blessing they are :)

Thanks much for the code.  I am under some time constraints on this project so will test this morning and let you know if I have further questions.  

I hope all is well with you.  I do have to get back into EE and answering some questions.  I miss it!    Warm regards.

Joanne
0
 
LVL 37

Author Comment

by:Joanne M. Orzech
ID: 39788348
omgosh!  This is EXACTLY what I needed.... thanks so much Graham.  You are the very best! :)
0
 
LVL 37

Author Closing Comment

by:Joanne M. Orzech
ID: 39788349
Quick and perfecto!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39788458
Thank you Joanne. It was a real pleasure.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question