Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 995
  • Last Modified:

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

0
Hawkvalley1
Asked:
Hawkvalley1
  • 2
1 Solution
 
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now