How to fill several content controls in a Word2007 template?

Hi All.

Trying to fill 2 diff. content controls on my template, with the following code I get the first one to fill but not the second one, any ideas?

Dim objcc As Microsoft.Office.Interop.Word.ContentControl
            For Each objcc In oDoc.ContentControls
                If objcc.Type = WdContentControlType.wdContentControlText Then
                    If objcc.Tag = "Units" Then
                        objcc.SetPlaceholderText(Nothing, Nothing, TextBox2.Text)
                        oDoc.Content.Paragraphs.Add()
                    End If
                    If objcc.Tag = "Narrative" Then
                        objcc.SetPlaceholderText(Nothing, Nothing, TextBox1.Text)
                    End If
                End If
                Exit For
            Next
'or
 Dim objcc As Microsoft.Office.Interop.Word.ContentControl
            For Each objcc In oDoc.ContentControls
                If objcc.Type = WdContentControlType.wdContentControlText Then
                    If objcc.Tag = "Units" Then
                        objcc.Range.Text = TextBox2.Text
                        oDoc.Content.Paragraphs.Add()
                    End If
                    If objcc.Tag = "Narrative" Then
                        objcc.Range.Text = TextBox1.Text
                        End If
                End If
                Exit For
            Next

Open in new window

LVL 9
Hawkvalley1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

irudykCommented:
The issue is the use of the Exit For.  Once the first content control is found, if it is a text box then its respective code is run otherwise nothing happens to the content control. Either way, the Exit For line is run whihc bypasses any further processing of the content controls.  So in essence, you only ever parse the first content control.
To correct, just remove that line from your code.

Dim objcc As Microsoft.Office.Interop.Word.ContentControl
            For Each objcc In oDoc.ContentControls
                If objcc.Type = WdContentControlType.wdContentControlText Then
                    If objcc.Tag = "Units" Then
                        objcc.SetPlaceholderText(Nothing, Nothing, TextBox2.Text)
                        oDoc.Content.Paragraphs.Add()
                    End If
                    If objcc.Tag = "Narrative" Then
                        objcc.SetPlaceholderText(Nothing, Nothing, TextBox1.Text)
                    End If
                End If
            Next
'or
 Dim objcc As Microsoft.Office.Interop.Word.ContentControl
            For Each objcc In oDoc.ContentControls
                If objcc.Type = WdContentControlType.wdContentControlText Then
                    If objcc.Tag = "Units" Then
                        objcc.Range.Text = TextBox2.Text
                        oDoc.Content.Paragraphs.Add()
                    End If
                    If objcc.Tag = "Narrative" Then
                        objcc.Range.Text = TextBox1.Text
                        End If
                End If
            Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hawkvalley1Author Commented:
You are absolutely right, I got that from an example online - it appeared to me that it would evaluate each content then exit, appearently not, thanks for the help...
0
Hawkvalley1Author Commented:
Thanks again...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.