#1
|
|||
|
|||
Finding specific Content Control based on both title and tag
I am a VBA novice using Office 2010.
I have a macro enabled document with all private subroutines under the ThisDocument module. The document has a series of content controls titled and tagged in a 2-dimension array like fashion, such as: [.Title = "1", .Tag = "a"], [.Title = "1", .Tag = "b"], [.Title = "1", .Tag = "c"], [.Title = "2", .Tag = "a"], [.Title = "2", .Tag = "b"], [.Title = "2", .Tag = "c"], [.Title = "3", .Tag = "a"], [.Title = "3", .Tag = "b"], [.Title = "3", .Tag = "c"] (The main reason for this is so I can run subroutines to clear the data in the content controls based on .Title or .Tag respectively.) I am writing code for the Document_ContentControlOnExit state, where I want to use the title of the content control being exited as a reference to select a certain other content control with the same title but a different tag. Example - I want to select the content control [.Title = "1", .Tag = "b"] to perform an operation on, by using the title of content control [.Title = "1", .Tag = "a"] on exit. My present thought process was to limit my initial subset of ContentControls to .Title = "1" with SelectContentControlsByTag, and then select the content control with .Tag = "b" - I am unclear how I would do this based on what I have read on the online Microsoft VBA documentation. A failed approach follows: (error is "Compile error: Method or data error not found" pointing to the last line of code) Code:
Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl, Cancel As Boolean) Dim i as String, iCC As ContentControls, jCC As ContentControl i = oCC.Title With oCC If .Tag = "a" Then Set iCC = ActiveDocument.SelectContentControlsByTag("b") Set jCC = iCC.SelectContentControlsByTitle(i)(1) === On a similar note, I wish to run a subroutine where operations are done in sequential order with reference to .Title, -> e.g. edit data of [.Title = "1", .Tag = "b"], then edit data of [.Title = "2", .Tag = "b"], etc.), with .Title being the dynamic variable and .Tag being constantly "b" in this example. My content control titles unfortunately are not in a complete numerical series (they run 1-12, E, F, then 19-20), and given they are strings I am unsure if a For statement would work. Would using a Select Case work best in this instance? |
#2
|
||||
|
||||
It's not exactly clear what determines the decision to process by tag or title. Perhaps something along the lines of:
Code:
Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl, Cancel As Boolean) Dim i As Long With ActiveDocument Select Case oCC.Tag Case "a" For i = 1 To .SelectContentControlsByTag(oCC.Title).Count .SelectContentControlsByTag(oCC.Title)(i).Range.Text = "" Next End Select End With End Sub Code:
Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl, Cancel As Boolean) Dim i As Long With ActiveDocument For i = 1 To .SelectContentControlsByTag(oCC.Tag).Count .SelectContentControlsByTag(oCC.Tag)(i).Range.Text = "" Next End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
I would be inclined to do somethiung like the following where you could setup the conditions for all the controls you want to process in this way. Here I have just processed those with the title '1".
Code:
Private Sub Document_ContentControlOnExitold(ByVal oCC As ContentControl, Cancel As Boolean) Dim jCC As ContentControl With oCC If .Title = "1" Then Select Case .Tag Case Is = "a" For Each jCC In ActiveDocument.ContentControls If jCC.Title = "1" And jCC.Tag = "b" Then jCC.Range.Select Exit For End If Next jCC Case Is = "b" For Each jCC In ActiveDocument.ContentControls If jCC.Title = "1" And jCC.Tag = "c" Then jCC.Range.Select Exit For End If Next jCC Case Is = "c" For Each jCC In ActiveDocument.ContentControls If jCC.Title = "2" And jCC.Tag = "a" Then jCC.Range.Select Exit For End If Next jCC End Select End If End With End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
|||
|
|||
I think the answer to your question is "no" you can't query both .Tag and .Title simultaneously to find the content control of interest.
For what is seems to me you are trying to do though, something like this might do. Code:
Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl, Cancel As Boolean) Dim collCCs As ContentControls Dim oCCTarget As ContentControl Select Case oCC.Tag Case "a" Set collCCs = ActiveDocument.SelectContentControlsByTag("b") For Each oCCTarget In collCCs If oCCTarget.Title = oCC.Title Then oCCTarget.Range.Select Exit For End If Next End Select End Sub |
#5
|
|||
|
|||
Thanks all for your prompt replies.
I ended up using Greg's method of defining a subset of content controls first, which has worked perfectly and I have adapted for other code in the same document. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Automating entries based on the entry in a Content Control | gsbpxw | Word VBA | 1 | 10-22-2018 12:45 PM |
VBA to provide text string with specific formating based on Drop down list (content control) | MP1989 | Word VBA | 4 | 07-30-2018 02:40 AM |
How to display tag in Content Control instead of title | vinbalraj | Word | 1 | 03-02-2018 08:55 AM |
Finding field names in a Word-Form created by content control | shammi_raj | Word | 1 | 03-10-2016 02:37 AM |
Code to Sum Column of Content Control Values In Specific Tables? | warbird | Word VBA | 2 | 07-13-2015 05:44 AM |