Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 01-30-2011, 08:31 PM
cksm4 cksm4 is offline Windows XP Office 2007
Advanced Beginner
 
Join Date: Aug 2010
Posts: 49
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, 09:48 PM
macropod macropod is online now Windows 7 32bit Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 10,036
macropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the rough
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
[MS MVP - Word]
Reply With Quote
  #3  
Old 01-30-2011, 10:08 PM
cksm4 cksm4 is offline Windows XP Office 2007
Advanced Beginner
 
Join Date: Aug 2010
Posts: 49
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, 10:14 PM
macropod macropod is online now Windows 7 32bit Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 10,036
macropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the rough
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
[MS MVP - Word]
Reply With Quote
  #5  
Old 01-30-2011, 10:33 PM
cksm4 cksm4 is offline Windows XP Office 2007
Advanced Beginner
 
Join Date: Aug 2010
Posts: 49
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-30-2011, 11:19 PM
macropod macropod is online now Windows 7 32bit Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 10,036
macropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the rough
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
[MS MVP - Word]
Reply With Quote
  #7  
Old 02-27-2011, 07:10 PM
cksm4 cksm4 is offline Windows XP Office 2007
Advanced Beginner
 
Join Date: Aug 2010
Posts: 49
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, 07:41 PM
macropod macropod is online now Windows 7 32bit Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 10,036
macropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the rough
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
[MS MVP - Word]
Reply With Quote
  #9  
Old 02-27-2011, 08:51 PM
cksm4 cksm4 is offline Windows XP Office 2007
Advanced Beginner
 
Join Date: Aug 2010
Posts: 49
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-27-2011, 11:45 PM
macropod macropod is online now Windows 7 32bit Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 10,036
macropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the rough
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
[MS MVP - Word]
Reply With Quote
  #11  
Old 03-01-2011, 11:43 AM
cksm4 cksm4 is offline Windows XP Office 2007
Advanced Beginner
 
Join Date: Aug 2010
Posts: 49
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, 01:07 PM
macropod macropod is online now Windows 7 32bit Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 10,036
macropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the rough
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
[MS MVP - Word]
Reply With Quote
  #13  
Old 03-02-2011, 08:53 AM
cksm4 cksm4 is offline Windows XP Office 2007
Advanced Beginner
 
Join Date: Aug 2010
Posts: 49
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
Reply
Please reply to this thread with any new information or opinions.

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 03: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


All times are GMT -7. The time now is 02:24 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft