Insert text into a content control

N

nostabo

I'm trying to automate the insertion of text into contracts using Word 2007.
I can do it with the legacy controls, but the new text content control won't
seem to do it.

The code that works for the legacy controls is:
ActiveDocument.FormFields("txtContractName").Result = txtContractName.Text

Where the txtContractName that supplies the value is on a form.

I'd like to use the more "modern" control. Is it possible?
 
G

Greg Maxey

ContentControls are indexed in the document. So

Sub Test()
ActiveDocument.ContentControls(3).Range.Text = "I'm CC index number 3"
End Sub

Each CC also has a unique ID number. You can get the number by selecting
the CC and run this code:

Sub GetCCID()
MsgBox Selection.Range.ContentControls(1).ID
End Sub

Then you can work with that unique CC using:

Sub Test()
ActiveDocument.ContentControls("##########").Range.Text = "Whatever text you
want the CC to show"
End Sub

Where "##########" is the unique ten digit ID.
 
N

nostabo

Thanks, Greg. But the next question is where do I find the CC's index? It
doesn't seem to be revealed in the "Properties" dialog. I know that I can
guess, but with 30 + CCs it could get tough to remember them all.
 
N

nostabo

Thanks, Doug,

I am using a userform to get the input needed to fill in the contract. The
question was how to find the index numbers for the CCs on the document. I
assume that they, like controls on a userform are indexed as they are
created, which confuses the order when they are added and deleted.
 
G

Greg Maxey

That's why I think it is best to work with the unique CC ID.

Each CC also has a unique ID number. You can get the number by selecting
the CC and run this code:

Sub GetCCID()
MsgBox Selection.Range.ContentControls(1).ID
End Sub

Then you can work with that unique CC using:

Sub Test()
ActiveDocument.ContentControls("##########").Range.Text = "Whatever text you
want the CC to show"
End Sub
 
D

Doug Robbins - Word MVP

Why use Content Controls? Especially in conjunction with a userform.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

nostabo

I am thinking that Content Controls would give a user the most flexibility as
far as automation and editability (if needed later to "tune-up" the input
without calling up the user form.) If not Content Controls, then what would
one use?
 
D

Doug Robbins - Word MVP

Have the userform create document variables so that the data is displayed in
DOCVARIABLE fields that you insert in the template where you were
contemplating inserting the content controls and after the code creates the
variables and updates the fields, have it unlink the DOCVARIABLE fields
which will convert the data displayed in them to ordinary text so that the
user can modify it if they need to.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

nostabo

Thanks, Doug...and Greg,

I overlooked using DOCVARIABLES in favor of editable controls, but as you
point out these are probably my best choice.

Now I'm off to look at inserting tables and some rather large chunks of text
into my document using DOCVARIABLES.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top