# VBA code to count # of Content Control Tags in a Microsoft Word Form and create array listing all the tag names



## athaung (Dec 15, 2015)

Hi,

I have a Word Form document with several Rich Text Content Control objects. I would like to write a program to go through the entire document and make a list of all the Content Control objects with tag names in it. I would like this list to be all of the tag names in the document. Finally, I would like to see a "print" of this array to verify that the list tags

I've been cobbling together code that I've seen from various google searches, and can't seem to get anything to work - right now I get a "Compile Error: Invalid Use of Property" msgbox. Here is what I have so far:


```
Sub GetCCByTag()Dim cc As ContentControl
Dim docCCs As ContentControl
Dim myDoc As Document
Dim arr() As Variant


myDoc = ActiveDocument


For Each cc In myDoc
    ReDim Preserve arr(1 To UBound(arr) + 1) As Variant
    arr(UBound(arr)) = cc.Tag
Next


End Sub
```

Would appreciate some help to i) go through the whole document and look at all content control objects with tag names and ii) create a list of all these names and "print" them so I can see what it includes


----------



## Macropod (Dec 16, 2015)

Try:

```
Sub GetCCTags()
Dim i As Long, StrOut As String
With ActiveDocument
  For i = 1 To .ContentControls.Count
    StrOut = StrOut & vbCr & i & vbTab & .ContentControls(i).Tag
  Next
End With
StrOut = "Index" & vbTab & "Tag" & StrOut
Documents.Add
ActiveDocument.Range.Text = StrOut
End Sub
```
The output goes to a new document that you can view and/or print.


----------



## athaung (Dec 16, 2015)

Macropod said:


> Try:
> 
> ```
> Sub GetCCTags()
> ...



Unfortunately, this code throws the same error "Compile error: Sub or Function not defined" and highlights "ContentControls" on the line where you define StrOut = StrOut & vbCr & I vbTab & _.ContentControl_s(i).Tag

Any other thoughts? Also, I'm not sure if capturing a list of these Content Control names in a string will work because I eventually want to loop through all of the Content Controls in the document to populate information from an Excel file!


----------



## Macropod (Dec 16, 2015)

Aside from some missing quotes on this line:
StrOut = "Index" & vbTab & "Tag" & StrOut
The code runs fine for me. Your results suggest a faulty Office installation. Try repairing it (via Start > Windows Control Panel > Programs > Programs & Features > Microsoft Office (version) > Change > Repair).

PS: I've now added the above corrections to the original post. In editing, I first erroneously 'corrected' your reply. Sorry about that - I've undone those changes, but the 'edit' note remains.


----------



## athaung (Dec 17, 2015)

Unfortunately, I don't think it's a faulty Office installation. I had our IT dept check my computer and then try to run the same macro on my personal computer at home. The error remains the same that Sub or Function is not defined as I described above. Any other ideas of what might be causing this?


----------



## Macropod (Dec 17, 2015)

As I've already indicated, the macro runs fine for me - even if you run it on a document with _no_ content controls. What version of Office are you running this on?


----------



## athaung (Dec 18, 2015)

Office 2010 on a Windows 7 Enterprise. I'm wondering whether there any references/additional settings I have to select to make this work. Does anyone have any other ideas? Can others run Paul's (Macropod) code without similar error issues?


----------



## Macropod (Dec 21, 2015)

I developed the code on Office 2010 Professional Plus on Windows 7 Ultimate. The code should work fine on Any Office version from 2007 on and on any Windows version that supports your Office version.


----------

