Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2011, 09:31 PM
cksm4 cksm4 is offline Move to next content control Windows XP Move to next content control Office 2007
Advanced Beginner
Move to next content control
 
Join Date: Aug 2010
Posts: 48
cksm4 is on a distinguished road
Default 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
Thanks!
Reply With Quote
  #2  
Old 01-30-2011, 10:48 PM
macropod's Avatar
macropod macropod is offline Move to next content control Windows 7 32bit Move to next content control Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
  #3  
Old 01-30-2011, 11:08 PM
cksm4 cksm4 is offline Move to next content control Windows XP Move to next content control Office 2007
Advanced Beginner
Move to next content control
 
Join Date: Aug 2010
Posts: 48
cksm4 is on a distinguished road
Default

Hello Paul,

It stops after the 1st instance. There could be many.
Reply With Quote
  #4  
Old 01-30-2011, 11:14 PM
macropod's Avatar
macropod macropod is offline Move to next content control Windows 7 32bit Move to next content control Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
  #5  
Old 01-30-2011, 11:33 PM
cksm4 cksm4 is offline Move to next content control Windows XP Move to next content control Office 2007
Advanced Beginner
Move to next content control
 
Join Date: Aug 2010
Posts: 48
cksm4 is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 01-31-2011, 12:19 AM
macropod's Avatar
macropod macropod is offline Move to next content control Windows 7 32bit Move to next content control Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
  #7  
Old 02-27-2011, 08:10 PM
cksm4 cksm4 is offline Move to next content control Windows XP Move to next content control Office 2007
Advanced Beginner
Move to next content control
 
Join Date: Aug 2010
Posts: 48
cksm4 is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 02-27-2011, 08:41 PM
macropod's Avatar
macropod macropod is offline Move to next content control Windows 7 32bit Move to next content control Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Hi Brock,
Quote:
I am needing to paste the value of each i and i+1 after the i+1
Please explain. I don't understand what you mean. I'm guessing that you want to paste the incrementing 'i' values into your document somewhere (or perhaps add them to the StrTmp strings), but I'm not sure.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 02-27-2011, 09:51 PM
cksm4 cksm4 is offline Move to next content control Windows XP Move to next content control Office 2007
Advanced Beginner
Move to next content control
 
Join Date: Aug 2010
Posts: 48
cksm4 is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 02-28-2011, 12:45 AM
macropod's Avatar
macropod macropod is offline Move to next content control Windows 7 32bit Move to next content control Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
  #11  
Old 03-01-2011, 12:43 PM
cksm4 cksm4 is offline Move to next content control Windows XP Move to next content control Office 2007
Advanced Beginner
Move to next content control
 
Join Date: Aug 2010
Posts: 48
cksm4 is on a distinguished road
Default

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
Reply With Quote
  #12  
Old 03-01-2011, 02:07 PM
macropod's Avatar
macropod macropod is offline Move to next content control Windows 7 32bit Move to next content control Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
  #13  
Old 03-02-2011, 09:53 AM
cksm4 cksm4 is offline Move to next content control Windows XP Move to next content control Office 2007
Advanced Beginner
Move to next content control
 
Join Date: Aug 2010
Posts: 48
cksm4 is on a distinguished road
Default

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
Thanks again!!
Reply With Quote
  #14  
Old 07-02-2019, 07:48 PM
dpaton05 dpaton05 is offline Move to next content control Windows 10 Move to next content control Office 2016
Novice
 
Join Date: Jul 2019
Posts: 19
dpaton05 is on a distinguished road
Default

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

Thread Tools
Display Modes


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
Move to next content control 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:55 AM.


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