Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to set 1st 4 Word Table Rows as Repeating Table Header using .Net C#

594 views
Skip to first unread message

BBFrost

unread,
Jun 23, 2003, 8:13:05 PM6/23/03
to
I've created a table using C# and the new office PIA interface.

The 1st 4 lines of the table constitute the table header.

I need to have these 4 header lines repeat as the table header on each new
document page.

In the word document opened by the C# app I can select the 1st 4 lines and
then
select the Table => "Heading Rows Repeat" option to get the header to appear
on each page.

Unfortunately I'm stuck when it comes to figuring out how to invoke the
"Heading Rows Repeat" option with C#.

Any help would be greatly appreciated!

Thanks in advance.

Barry Frost
Oregon


Greg Ellison [MS]

unread,
Jun 25, 2003, 11:36:30 AM6/25/03
to
Hello Barry,

Take a look at the HeadingFormat Property in the Word object model. This is
from the Word VBA help:

HeadingFormat Property:

True if the specified row or rows are formatted as a table heading. Rows
formatted as table headings are repeated when a table spans more than one
page. Can be True, False or wdUndefined. Read/write Long.

Example
This example creates a 5x5 table at the beginning of the active document
and then adds the table heading format to the first table row.

Dim rngTemp As Range
Dim tableNew As Table

Set rngTemp = ActiveDocument.Range(0, 0)
Set tableNewe = ActiveDocument.Tables.Add(rngTemp, 5, 5)

tableNew.Rows(1).HeadingFormat = True
This example determines whether the row that contains the insertion point
is formatted as a table heading.

If Selection.Information(wdWithInTable) = True Then
If Selection.Rows(1).HeadingFormat = True Then _
MsgBox "The current row is a table heading"
Else
MsgBox "The insertion point is not in a table."
End If

For information and sample code for integrating Office with Visual Basic,
Visual C++, Internet Scripts, and other programming languages, please see
http://msdn.microsoft.com/library/techart/VSOfficeDev.htm. This site
contains the most up-to-date information for using developer tools for
Office integration and extensibility.

For additional information about programming Visual Studio .NET solutions
involving Microsoft Office, please see the following link:

Q311452 - INFO: Develop Microsoft Office Solutions with Visual Studio .NET
http://support.microsoft.com/default.aspx?scid=KB;en-us;311452

Best regards,
Greg Ellison
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

--------------------
>From: "BBFrost" <barry.b.frost@remove_this.wrd.state.or.us>
>Subject: How to set 1st 4 Word Table Rows as Repeating Table Header using
.Net C#
>Date: Mon, 23 Jun 2003 17:13:05 -0700
>Newsgroups: microsoft.public.office.developer.automation

BBFrost

unread,
Jun 25, 2003, 2:04:54 PM6/25/03
to
Greg,

Thanks for the response!

I found the VBA example you quoted and I've been using it as
a reference.

Unfortunately I've found that the VBA examples don't seem to
translate well to C# (i.e. non-default paramaters & hidden methods
like set_Item, etc)

I've since posted another message in the microsoft.public.dotnet.csharp
group listing a code snippet of what I was trying to accomplish. see
"Interop Word C# - problem using Rows[1].HeadingFormat = 1"

I'll check out the URL's you suggested, I definitely need all the help
I can get at this point. I'm definitely in over my head and essentially
programming using the 'PLEASE GOD! Let it work this time' method.

Is there a better group to post Word Interop questions?? I've
know about microsoft.public.dotnet.csharp & microsoft.
public.dotnet.framework.interop.

Thanks in advance.

Barry Frost
Oregon

"Greg Ellison [MS]" <gr...@online.microsoft.com> wrote in message
news:8qdDS#yODHA...@cpmsftngxa06.phx.gbl...

Greg Ellison [MS]

unread,
Jun 26, 2003, 1:30:36 PM6/26/03
to
Hello Barry,

This group is OK for posting Word automation questions. There are also
Word VBA newsgroups if the issue is specific to Word VBA.

To help you with the C# question, I've pasted a sample for you. I've
tested this on my Office XP machine and it worked fine. Notice I set
HeadingFormat = -1 instead of 1:

using Word = Microsoft.Office.Interop.Word;

private void button1_Click(object sender, System.EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;

//Start Word and make it visible to user:
Word._Application oWord;
oWord = new Word.Application();
oWord.Visible = true;

//Create a new document:
Word._Document oDoc;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

//Add a new table (100 rows by 3 columns):
Word.Selection oSel = oWord.Selection;
object oTableBehavior = Word.WdDefaultTableBehavior.wdWord9TableBehavior;
object oFitBehavior = Word.WdAutoFitBehavior.wdAutoFitFixed;
Word.Table oTable = oDoc.Tables.Add(oSel.Range,100,3,
ref oTableBehavior, ref oFitBehavior);

//Set table properties, etc.
object oStyle = "Table Grid";
oTable.set_Style(ref oStyle);
oTable.ApplyStyleHeadingRows = true;
oTable.ApplyStyleLastRow = true;
oTable.ApplyStyleFirstColumn = true;
oTable.ApplyStyleLastColumn = true;

//Add column headings to row 1:
oTable.Cell(1,1).Range.Text = "My Column Heading 1";
oTable.Cell(1,2).Range.Text = "My Column Heading 2";
oTable.Cell(1,3).Range.Text = "My Column Heading 3";

// Apply row 1 as heading. Use -1 instead of 1:
oTable.Rows[1].HeadingFormat = -1;
}


If you're automating Office XP and not Office 2000 or earlier, you should
use the Office XP PIAs. For more information see the following:

http://www.microsoft.com/downloads/details.aspx?FamilyId=C41BD61E-3060-4F71-
A6B4-01FEBA508E52&displaylang=en

--------------------

BBFrost

unread,
Jun 30, 2003, 12:28:27 PM6/30/03
to
Greg,

Many Thanks!!! That did it! I now have repeating headings. Awsome!

Is there some place in the existing documentation that I could have learned
that the enum value was -1??

I've also found that repeating the HeadingFormat command for each of the
first
4 table rows will cause those 4 rows to become repeating table headers.

oTable.Rows[1].HeadingFormat = -1;

oTable.Rows[2].HeadingFormat = -1;
oTable.Rows[3].HeadingFormat = -1;
oTable.Rows[4].HeadingFormat = -1;

While setting the HeadingFormat individually for each row is effective it
seems awkward.

I'm pretty sure this could be done by selecting the 1st 4 table rows and
then
setting the HeadingFormat for the selections. Unfortunately I have not yet
found an example that explains how to select a subset of table rows.

The documuentation for the range command suggests that one can have a
"range of rows" but I haven't stumbled across a way (with C#) to select a
specific set of rows from a table.

I've tried guessing at a number of iterations similar to

int iSelectBeginRow = 10;
int iSelectEndRow = 15;

Word.Range rng = (iSelectBeginRow, iSelectEndRow)
Word.Table.Select tblSelect = oTable.Range(rng).Rows.Select;

But I havn't yet stumbled across the correct combination. Can you point
me to an example that would give me a clue as to how selecting a subset
of table rows is accomplished??

Many thanks for the help you've already provided and any additional
hints you may be willing to provide.

Sincerely,

Barry Frost
Oregon


"Greg Ellison [MS]" <gr...@online.microsoft.com> wrote in message

news:zxoUtiAP...@cpmsftngxa06.phx.gbl...

Greg Ellison [MS]

unread,
Jun 30, 2003, 3:37:22 PM6/30/03
to
Hello Barry,

You're quite welcome! I'm glad the code sample was helpful. You asked how
I knew to use -1 for the HeadingFormat. The way I figured it out was to
first, manually create a table in the Word document that has the heading
rows set. Then, while in debug mode I checked the HeadingRow property to
see what its value was. It was -1, so then I knew to use -1 in my code to
set the property.

Here's an example of setting the HeadingFormat for the first four rows.
From the previous sample, replace this line:

oTable.Rows[1].HeadingFormat = -1;

with:

// Select first row of table:
oTable.Rows[1].Select();

// Extend 3 more lines from current row:
object oUnit = Word.WdUnits.wdLine;
object oCount = 3;
object oExtend = Word.WdMovementType.wdExtend;
oSel.MoveDown(ref oUnit, ref oCount, ref oExtend);

// Apply HeadingFormat to the selected rows:
oSel.Rows.HeadingFormat = -1;

// Change selection to first cell:
object oDirection = Word.WdCollapseDirection.wdCollapseStart;
oSel.Collapse(ref oDirection);

In addition to the other link I mentioned, you may also want to keep this
one handy:

222101 - HOWTO: Find and Use Office Object Model Documentation
http://support.microsoft.com/default.aspx?scid=KB;en-us;222101

Best regards,
Greg Ellison
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

--------------------
>From: "BBFrost" <barry.b.frost@remove_this.wrd.state.or.us>
>Subject: Re: How to set 1st 4 Word Table Rows as Repeating Table Header
using .Net C#

>Date: Mon, 30 Jun 2003 09:28:27 -0700
>Newsgroups: microsoft.public.office.developer.automation

BBFrost

unread,
Jun 30, 2003, 4:35:38 PM6/30/03
to
Wow! THIS IS AWSOME STUFF!!! Thanks BIG TIME!!!

It would have taken ages for me to figure out the select x rows from a table
incantation. REALLY APPRECIATE the code snippet.

I'll see if I can duplicate your 'reverse engineering' technique to
determine
the enum values. That'll come in VERY handy in the future.

I knew about the
http://support.microsoft.com/default.aspx?scid=KB;en-us;222101 url
and I've zeroed in on the vbawd10.chm & xpautomation.chm help files as being
the most
useful Word automation.

Again, thanks for the quick response and excellent help and examples!

Barry Frost
Oregon.

"Greg Ellison [MS]" <gr...@online.microsoft.com> wrote in message

news:h3QgL8zP...@cpmsftngxa09.phx.gbl...

0 new messages