Inserting content control in Word 2007 Footer using Word PIA in C#

S

Srinivas

Hi All,

I programmatically insert a plain text content control in a footer with
below code.


Microsoft.Office.Interop.Word.Document wordDocument =
application.Documents.Open( ref fileName, ref objFalse, ref objReadOnly, ref
objFalse, ref objMissing,
ref objMissing, ref objFalse,
ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref
ObjVisible, ref objFalse, ref objMissing, ref objTrue,
ref objMissing ) as
DocumentClass;

//cursor is focused some where in Footer before I call this code.
object range = wordDocument.ActiveWindow.Selection.Range;
ContentControl cc =
wordDocument.ContentControls.Add(WdContentControlType.wdContentControlText,
ref range);
cc.Tag = "myContentControl"


Here wordDocument.ContentControls.Add() returns a content control without
adding it to the wordDocument.ContentControls collection.
The wordDocument.ContentControls.Count remains same before and after.

Strangely, I could see the same conten control in the collection with the
following code:

ContentControls ccs =
wordDocument.SelectContentControlsByTag("myContentControl");



As I need to iterate wordDocument.ContentControls in different parts of my
application for all content controls (without bothering about tag)in the
document,

Could you please suggest me how I can add it(CC in footer) to
wordDocument.ContentControls or a collection where I can retrieve it later.

Thank in Advance,
Srinivas
ps: I have same problem inserting a content control in a Text Field.
 
C

Cindy M.

Hi Srinivas,
Here wordDocument.ContentControls.Add() returns a content control without
adding it to the wordDocument.ContentControls collection.
The wordDocument.ContentControls.Count remains same before and after.

Strangely, I could see the same conten control in the collection with the
following code:

ContentControls ccs =
wordDocument.SelectContentControlsByTag("myContentControl");

As I need to iterate wordDocument.ContentControls in different parts of my
application for all content controls (without bothering about tag)in the
document,
Could you please suggest me how I can add it(CC in footer) to
wordDocument.ContentControls or a collection where I can retrieve it later.

The reason you're getting these results is that when you query
wordDocument.contentControls.Count you're actually accessing only the main
body of the document. You'll see this same behavior with pretty much every
property or collection.

The FAQ example of this is Find.Execute. In the UI, Find cycles across all
parts of the document, by default, rather than running only in the current
"Story". When developer uses it, it applies ONLY to the current "Story".
Besides the main body of the document, header and footers, footnotes, end-
notes and text in Shapes are "Stories".

To deal with this, the object model provides the concept of the StoryRange.
If you look this up in the (VBA) Help, then follow related links, you'll see
a couple of examples that demonstrate how to work through all available
StoryRanges in a document. You'll also find a discussion (again, in VBA)
here:

http://word.mvps.org/faqs/macrosvba/FindReplaceAllWithVBA.htm

If your additional ranges are only headers and footers, another way to go
about this would be to loop all the sections in the document, querying the
ranges of the three different types of header and of footer.

Why Document.SelectContentControlsByTag (or by Title) don't follow this
pattern I don't know. It was certainly a Design decision. I suppose because
they already made it so complicated to select individual content controls
they perhaps thought that would be the proverbial straw... Or perhaps they
simply didn't realize at the time that they were being inconsistent.

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 :)
 
S

Srinivas

Hello Cindy,

Thank you very much for the point-by-point explanation. I had this code
written 2 years ago and as far as I remember, the Document.ContentControls
collection contained the content controls from footer as well.

Anyway, I need access to content controls placed all over the document, for
that as you suggested, iterating through StoryRanges perfectly works.

Best regards,
Srinivas
 
I

Irfan Shaikh (irfan.shaikh

Use Document.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary)

That will give you headerfooters collection

loop it and check with ISHeader property false in order to get footer
you can get range object from HeaderFooter object

Add content control to its range

CC =
HeaderFooter.Range.ContentControls.Add(WdContentControlType.wdContentControlDate, HeaderFooter.Range)

Thanks,

Irfan Shaikh
 

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