Wrapper class for ContentControls (implementing Text property)

  • Thread starter Jens Gyldenkærne Clausen
  • Start date
J

Jens Gyldenkærne Clausen

Hi group. I am working with ContentControls in Word 2007 and have now
(once again) stumbled upon something peculiar.

The basic Microsoft.Office.Tools.Word.ContentControl class - the type
that can represent all eight types of content controls - has lots of
members, some of them specific to certain types (such as
DateCalendarType and ShowingPlaceholderText). But one very important
property is missing - a simple .Text property to expose the textual
content in a PlainTextContentControl, RichTextContentControl,
DatepickerContentControl etc. Why is that??

I'm programming a solution where I need to access all content controls
in a Word template - and for every textual content control be able to
read and set the content. This seems to be frustratingly difficult. I'm
now trying to write a wrapper class implementing the missing .Text
property - but in doing that, I discovered that 1) the ContentControl
class is sealed, making it impossible to expand; 2) the
ContentControlBase class has a note stating that one shouldn't derive
own types from it.

What would be "best practice" in this case? I'm thinking of

A) Ignore the note and derive from ContentControlBase since I need a
handle to the Text property

B) Drop the wrapper class and handle the Text set/get with type probing
and duplicated internal code

C) Try to implement some kind of cast functionality, using the
PlainTextContentControl as the lowest denominator (in my programmatic
manipulations I have no need for methods/properties other than the ones
supported by a PlainTextContentControl.

Reference link:
http://msdn.microsoft.com/library/microsoft.office.tools.word.contentcontrol(en-us).aspx
 
J

Jay Freedman

Hi Jens,

Try using the control's .Range.Text property. I think it will do what you
want -- at least, it appears to do so in VBA.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jens Gyldenkærne Clausen

Jay Freedman wrote
Try using the control's .Range.Text property. I think it will do what you
want -- at least, it appears to do so in VBA.

Well - the .Range property does indeed provide a handle to the textual
content in the control; but unfortunately it is a readonly property. I
need to be able to set the text as well as reading it (this is working
fine in my current code where I'm only working with
PlainTextContentControls, but the final template will have a mixture of
PlainText-, RichText- and DatePickerContentControls. For all of these
control types the same basic action (using get and set at the text
content in the control) is to be added on the Enter/Exit events.

To make things even more confusing, there is a note in the
documentation of the ContentControl.Range property referring to the
(nonexisting) .Text property:

| To access only the text that is displayed in the ContentControl, use
| the Text() property.

http://msdn.microsoft.com/library/microsoft.office.tools.word.contentcontrol.range

Who ate my Text property?
 

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