#1
|
|||
|
|||
Move to next content control
Hello,
Does anyone know how to move to the next content control? I have a content control that I find based on it's tag. What I need to do is move to the next one after it is found. Code:
Dim strTmp As String Dim cc As ContentControl For Each cc In ActiveDocument.contentcontrols If cc.Tag = "ContainerContents" Then strTmp = cc.Range.Text 'Here is where I need to go to the next cc and select the contents strTmp = strTmp & Selection.Range.Text End With End If Next |
#2
|
||||
|
||||
Hi Brock,
You could use something like: Code:
Dim strTmp As String, i As Integer With ActiveDocument For i = 1 To .ContentControls.Count If .ContentControls(i).Tag = "ContainerContents" Then strTmp = .ContentControls(i).Range.Text & .ContentControls(i + 1).Range.Text Exit For End If Next End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hello Paul,
It stops after the 1st instance. There could be many. |
#4
|
||||
|
||||
Hi Brock,
If the Content Controls both have the same tag, you could use something like: Code:
Dim strTmp As String With ActiveDocument.SelectContentControlsByTag("ContainerContents") strTmp = .Item(1).Range.Text & .Item(2).Range.Text End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
It works!! I removed the Exit For and got the code to move to the next instance in the manner I needed. Basically, I need it to create the string for each tag it finds. Thanks again Paul, much appreciated!
Brock |
#6
|
||||
|
||||
Hi Brock,
The 'Exit For' was there because your post implied that you only wanted the very next Content Control, not all of them, and you didn't indicate anything about whether the next Content Control had the same tag. Since it seems only some of the Content Controls have the 'ContainerContents' tag, it would be more efficient to do it this way: Code:
Dim strTmp As String, i As Integer With ActiveDocument.SelectContentControlsByTag("ContainerContents") For i = 1 To .Count strTmp = strTmp & .Item(i).Range.Text Next End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Hello Paul,
The code works in creating a string of all values of i and i+1 found throughout the document; however, I am needing to paste the value of each i and i+1 after the i+1. And then move the next i with the matching tag. I am still working with adding indexes so I need to index the value of each i and i+1 where it is found in the document. I am unable to set focus on the content control so I can past the value. Can you give me some direction? Thanks! Brock |
#8
|
||||
|
||||
Hi Brock,
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Hey Paul,
Since the value of i and i+1 will be used in an index XE field, I have to insert their value where each i is found so that the page numbering in the index is correct. And then move to the next instance of i which will most likely be on another page. Thanks! Brock |
#10
|
||||
|
||||
Hi Brock,
But where is the 'after' - within the content control or in the body of the document? If it's the latter, the document will have to be unprotected first.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Hey Paul,
The after will be at the beginning (before) the i. That way I know the XE is set on the page where i is located. I though about somehow selecting i (after i+1 was set) and moving back 1 space. That should put me right before the i. I am avoiding inserting the XE within the control as the user could manipulate it then. The code that runs other commands before this already ungroups (and later groups) the controls. Thanks! Brock |
#12
|
||||
|
||||
Hi Brock,
I'm still not understanding exactly what you're trying to do, but maybe the following will help. It puts a tab, then 'i', into the body of the document immediately after the content control it relates to: Code:
Sub Test() Dim strTmp As String, i As Integer, Rng As Range With ActiveDocument.SelectContentControlsByTag("ContainerContents") For i = 1 To .Count strTmp = strTmp & .Item(i).Range.Text Set Rng = .Item(i).Range With Rng .Collapse (wdCollapseEnd) .Move wdCharacter, 1 .InsertAfter vbTab & i End With Next End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Hey Paul,
Yes, that helped a lot. This is the first time I used a range in this manner and I needed your example to put me in the right direction. Here is the code that does what I need: Code:
With ActiveDocument For i = 1 To .ContentControls.Count If .ContentControls(i).Tag = "ContainerContents" Then strTmp = .ContentControls(i).Range.Text & .ContentControls(i + 1).Range.Text Set Rng = .ContentControls(i + 1).Range With Rng .Collapse (wdCollapseEnd) .Move wdCharacter, 1 .InsertAfter strTmp End With strTmp = "" End If Next End With |
#14
|
|||
|
|||
Hi Paul,
I just found this thread and it looked similar to what I am looking for but am struggling to find an answer. Can you comment on this thread please? https://www.msofficeforums.com/word-...rols-form.html Thanks Paul, Dave |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Table of Content Eat up RAM Resources | Tommy | Word | 0 | 08-23-2010 10:08 PM |
need to grab content of certain fields? | erik2000 | Word | 0 | 02-19-2010 04:19 AM |
Package for CD with links to Windows media player content and Flash Shockwave content | hectorh | PowerPoint | 4 | 10-15-2009 12:22 PM |
share content between documents? | albytrott | Word | 1 | 10-08-2009 08:27 AM |
Templates: automatic text generation from Rich Text content control | Chickenmunga | Word | 0 | 10-01-2008 11:16 AM |