Add a textbox(shape) in header/footer

F

Frenchy

Hello,

I'm developing an add-in for Word 2003 using VS2003 (Extensibility Projects).

I have to add a textbox in the header and footer.

I tried to record a macro to see how Word does it, but when I record the
macro I can't add the textbox.

So I tried to add a textbox with the anchor of the header. But It says taht
this range isn't part of the document :(

Here's my code:

Word.Shape ShapeHead;
object anchor =
this.appWord.ActiveDocument.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
ShapeHead =
appWord.ActiveDocument.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,0,0,50,50, ref anchor);

Does anyone could tell me how to add a textbox in the header/footer ?

In fact I'm using a textbox to be able to update it later. Because the
textbox is a shape and a shape can be accessed by its name. So if you know
other components that do the same, it could help me too.

Thanks
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RnJlbmNoeQ==?=,
Word.Shape ShapeHead;
object anchor =
this.appWord.ActiveDocument.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeader
FooterPrimary].Range;
ShapeHead =
appWord.ActiveDocument.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation
msoTextOrientationHorizontal,0,0,50,50, ref anchor);
Does anyone could tell me how to add a textbox in the header/footer ?
Careful reading of the Help on the Shapes collection reveals "A collection of
Shape objects that represent all the shapes in a document or all the shapes in
all the headers and footers in a document." IOW, the "document" is not strictly
the same as the "headers and footers". VBA lets you use it that way, but C# is
not very lenient :) Indeed, even with VBA, there are issues when trying to
manage graphics in documents with multiple headers and footers...

applying this knowledge, I came up with the following. Note how I declare a
HeaderFooter object and use that as the target of the Shapes.AddTextBox method:

wd.Shape ShapeHead;
wd.HeaderFooter HF =
wdDoc.Sections[1].Headers[wd.WdHeaderFooterIndex.wdHeaderFooterPrimary];
HF.Range.Paragraphs[1].Range.Text = "TEST";
object anchor =
HF.Range.Paragraphs[1].Range;
//anchor.Collapse(ref (object)
wd.WdCollapseDirection.wdCollapseStart);
//anchor.Text = "New text ";
ShapeHead =
HF.Shapes.AddTextbox(

Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
0,0,50,50, ref anchor);
ShapeHead.TextFrame.TextRange.Text = "Test!";


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 
F

Frenchy

Thanks a lot, It works :)

Cindy M -WordMVP- said:
Hi =?Utf-8?B?RnJlbmNoeQ==?=,
Word.Shape ShapeHead;
object anchor =
this.appWord.ActiveDocument.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeader
FooterPrimary].Range;
ShapeHead =
appWord.ActiveDocument.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation
.msoTextOrientationHorizontal,0,0,50,50, ref anchor);
Does anyone could tell me how to add a textbox in the header/footer ?
Careful reading of the Help on the Shapes collection reveals "A collection of
Shape objects that represent all the shapes in a document or all the shapes in
all the headers and footers in a document." IOW, the "document" is not strictly
the same as the "headers and footers". VBA lets you use it that way, but C# is
not very lenient :) Indeed, even with VBA, there are issues when trying to
manage graphics in documents with multiple headers and footers...

applying this knowledge, I came up with the following. Note how I declare a
HeaderFooter object and use that as the target of the Shapes.AddTextBox method:

wd.Shape ShapeHead;
wd.HeaderFooter HF =
wdDoc.Sections[1].Headers[wd.WdHeaderFooterIndex.wdHeaderFooterPrimary];
HF.Range.Paragraphs[1].Range.Text = "TEST";
object anchor =
HF.Range.Paragraphs[1].Range;
//anchor.Collapse(ref (object)
wd.WdCollapseDirection.wdCollapseStart);
//anchor.Text = "New text ";
ShapeHead =
HF.Shapes.AddTextbox(

Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
0,0,50,50, ref anchor);
ShapeHead.TextFrame.TextRange.Text = "Test!";


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)


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