Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-29-2019, 01:05 PM
ZaSpai ZaSpai is offline Finding specific Content Control based on both title and tag Windows 8 Finding specific Content Control based on both title and tag Office 2010
Novice
Finding specific Content Control based on both title and tag
 
Join Date: Mar 2019
Posts: 2
ZaSpai is on a distinguished road
Default 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)
Is there a method by which I can query both .Tag and .Title simultaneously to find the content control of interest, or am I restricted to filtering based on one at a time? If the latter, how would I choose the correct control and how would this be coded (compared with my attempt above)?

===

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?
Reply With Quote
  #2  
Old 03-29-2019, 04:27 PM
macropod's Avatar
macropod macropod is offline Finding specific Content Control based on both title and tag Windows 7 64bit Finding specific Content Control based on both title and tag Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
or:
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]
Reply With Quote
  #3  
Old 03-30-2019, 02:05 AM
gmayor's Avatar
gmayor gmayor is offline Finding specific Content Control based on both title and tag Windows 10 Finding specific Content Control based on both title and tag Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #4  
Old 03-30-2019, 10:37 AM
gmaxey gmaxey is offline Finding specific Content Control based on both title and tag Windows 10 Finding specific Content Control based on both title and tag Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 03-31-2019, 11:37 AM
ZaSpai ZaSpai is offline Finding specific Content Control based on both title and tag Windows 8 Finding specific Content Control based on both title and tag Office 2010
Novice
Finding specific Content Control based on both title and tag
 
Join Date: Mar 2019
Posts: 2
ZaSpai is on a distinguished road
Default

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.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding specific Content Control based on both title and tag 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 specific Content Control based on both title and tag Finding field names in a Word-Form created by content control shammi_raj Word 1 03-10-2016 02:37 AM
Finding specific Content Control based on both title and tag Code to Sum Column of Content Control Values In Specific Tables? warbird Word VBA 2 07-13-2015 05:44 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:42 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft