ContentControls ID

K

KWarner

We should be able to set the ContentControls ID so that it can be referenced
from code. i.e. ThisDocument.Range.ContentControls("MyCC")

Thanks for your consideration.

----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/com...72880e97&dg=microsoft.public.word.vba.general
 
G

Greg Maxey

You can't set the CC unique ID as it is assigned automatically when
the CC is created. You can get the ID with:

Sub GetUniqueID()
'Click on the CC tab to select the CC
MsgBox Selection.ContentControls(1).ID
End Sub

And then reference it is code with:

Sub SetCCContentByID()
ThisDocument.ContentControls("242079516").Range.Text = "Some text
here"
End Sub
 
K

KWarner

That's just my point. I don't want to have to click on it to get it's ID. For
instance, if CC("qty") is changed, then I want to be able to change
CC("amtLeft") from code. I know this example is not really suited for word,
but I am working on a form template where that type of scenario would come in
very handy. As it is, I have to do some workarounds to find the CC that I
want to change from code (and make sure I get the right one). Besides that,
If I wanted to go through and get each CC's ID it still wouldn't be very good
programming practices, what with descriptive names and all. Somehow
CC("138943") doesn't mean as much to me in code as CC("TheOneIwant") does.
While we're on the subject, It would be nice if we could give out Tables a
unique name also.
 
G

Greg Maxey

Then why don't you use the CC.Title:

Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel
As Boolean)
Select Case CC.Title
Case Is = "The One I Want (qty.)"
If IsNumeric(CC.Range.Text) Then
ThisDocument.SelectContentControlsByTitle("The One I Want
(amt.)").Item(1).Range.Text = Val(CC.Range.Text) * 4
Else
MsgBox "The value in the qty. control must be numeric"
End If
End Select
End Sub


That's just my point. I don't want to have to click on it to get it's
ID. For instance, if CC("qty") is changed, then I want to be able to
change CC("amtLeft") from code. I know this example is not really
suited for word, but I am working on a form template where that type
of scenario would come in very handy. As it is, I have to do some
workarounds to find the CC that I want to change from code (and make
sure I get the right one). Besides that, If I wanted to go through
and get each CC's ID it still wouldn't be very good programming
practices, what with descriptive names and all. Somehow CC("138943")
doesn't mean as much to me in code as CC("TheOneIwant") does. While
we're on the subject, It would be nice if we could give out Tables a
unique name also.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
K

KWarner

I do. Unfortunately, you can't direct reference a CC using it's title. You
have to loop through all of them in the doc or table or wherever. And besides
that, the CC.Title doesn't have to be unique. You can have 15 different CC's
all titled "myName". I don't understand why you think the way it is is such a
great thing. As far as I know, a ContentControl is the only (or one of the
very, very few) objects that can't be assigned a name/ID and then referenced
that way. This was just a suggestion to MS to make CC's as easy to use as
other objects.
I really would like to know why you believe the way CC's are assigned a
number ID is good and how it benefits the developer.
 
G

Greg Maxey

Wait a minute. I didn't say anything was "such a great thing." I simply
tried to offer you some alternatives that might work since what you suggest
and want doesn't?

As for getting serious attention from Microsoft on your suggestion I won't
hold my breathe. Hold yours if you want to but CCs are riddled with bugs as
it is. I doubt they will give serious attention to making them better
before they give some attention to making them work. From what I can't see
they aren't doing much about that.

I am not implying that you are not a serious, highly intelligent developer.
I'm neither, but I have spent many, many hours trying make something useful
out of ContentControls and this could be a stretch but perhaps you don't
understand them as well as you think.

What do you mean by "can't be assigned a name/ID and then referenced that
way?" and "You have to loop through all of them in the doc or table or
wherever?".

We both know it can't be assigned an ID (your losing sleep over it and I
don't care) but it can certainly be assigned a name (If you are willing to
let a title be a name.)

Sub AddTitledCC()
Dim oCC As ContentControl
Set oCC = ThisDocument.ContentControls.Add(wdContentControlText,
Selection.Range)
oCC.Title = "CCTitle"
End Sub

Fair enough?

Then:

Sub ReferenceCCByTitle()
ThisDocument.SelectContentControlsByTitle("CCTitle").Item(1).Range.Text =
"Did this not reference the title"
End Sub

Is that not referencing the CC by name in code?

As you know the a you can have multiple CCs with the same title. If that is
the case then if you want to want to work with one of them (any of them my
title) then you have to include its Item index.

If you had 15 different CCs titled "myName" then it seems you would want all
of them to contain the same data. After all, my name is Greg if it appears
on the first page or last page of a document and I assume yours wouldn't
chang between pages. If that is the case then the all the myName CCs should
be mapped to customXML so that if you change one with code then they all
change.



I do. Unfortunately, you can't direct reference a CC using it's
title. You have to loop through all of them in the doc or table or
wherever. And besides that, the CC.Title doesn't have to be unique.
You can have 15 different CC's all titled "myName". I don't
understand why you think the way it is is such a great thing. As far
as I know, a ContentControl is the only (or one of the very, very
few) objects that . This was just a suggestion to MS to make CC's as easy
to
use as other objects.
I really would like to know why you believe the way CC's are assigned
a number ID is good and how it benefits the developer.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
K

KWarner

This thread started out as a suggestion for MS. They asked for suggestions,
and that's all this was. I agree that CC's still need some serious attention.
As for getting serious attention from Microsoft on your suggestion I won't
hold my breathe.

Thank you for this. This is exactly what I was looking for.
 
G

Greg Maxey

You weren't the first that has spent time and frustration looking for it.
Glad I could help.

This thread started out as a suggestion for MS. They asked for
suggestions, and that's all this was. I agree that CC's still need
some serious attention.

Thank you for this. This is exactly what I was looking for.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
D

Doug Robbins - Word MVP

I am not sure of the exact path that you follow to get to the screen in
which you make a post that contains the "canned" text

----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

but they do show up here from time to time. The real purpose of these
newsgroups however is to obtain peer support in the use of the software.

--
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
 
C

Cindy M.

Hi =?Utf-8?B?S1dhcm5lcg==?=,
Besides that,
If I wanted to go through and get each CC's ID it still wouldn't be very good
programming practices, what with descriptive names and all. Somehow
CC("138943") doesn't mean as much to me in code as CC("TheOneIwant") does.
While we're on the subject, It would be nice if we could give out Tables a
unique name also.
1. There's a valid reason for ContentControl IDs to be read-only. The ID is a
GUID, which means the chances of it ever conflicting with any other ID value are
slim to non-existant. This was a consious design decision, so I very much doubt
it will be changed.

2. If I needed to access ContentControls by ID I'd probably do a *single* loop
through the document and create a collection that stores the ID and the Title
(and tag and whatever else is of interest). Then I could reference using (for
example) the Title which would give me the ID so that I can address the
contentControl directly. That's valid "programming practice" and one loop
through a document isn't that resource-intensive...

3. To name a table: Select the table and apply a bookmark to it. Or put it in a
ContentControl.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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