How to select the parent table?

F

Frenchy

Hello, I'm developping an add-in with C# .net

I'd like to select the parent table of a specific table. i've found the
NestingLevel property, but It's read-only.

i tried to use an other property: Parent
But it gives an object and I can't use It as a table:

Word.Table mytbl =
this.appWord.ActiveDocument.ActiveWindow.Selection.Range.Tables[1];

Word.Table tbltest = (Word.Table)mytbl.Parent;

^->doesn't work.

So...I get the first table selected by the user but I'd like to get the
parent table. how could i do?


Thanks,

Frenchy
 
S

Shauna Kelly

Hi Frenchy

I used the code below for something similar a while ago. I seem to remember
that it worked reliably, although I haven't tested it recently. I hope it's
useful.


Function GetParentTable(nLevel As Integer) As Table
'Author: Shauna Kelly, February 2003
'Purpose: Return reference to a parent table at nesting
'level nLevel. 'If nLevel = 1, then this returns a reference
'to the highest level of table in which the selection resides.
'If the selection is not within a table, this returns Nothing.
'Note that the selection is not changed by this function.

Dim oSelectionTable As Table
Dim nNestingLevel As Integer
Dim oRange As Range

If Selection.Information(wdWithInTable) Then
'Get the current table
Set oSelectionTable = Selection.Tables(1)

'Capture the current table's nesting level and set a
'range to start working with.
With oSelectionTable
nNestingLevel = .NestingLevel
Set oRange = .Range.Cells(1).Range.Characters(1)
End With

'Keep moving the range one character back until the
'character's table's nesting level reaches your required
'level
Do While nNestingLevel > nLevel
oRange.MoveStart Unit:=wdCharacter, Count:=-1
nNestingLevel = oRange.Tables(1).NestingLevel
Loop

'Set return value
Set GetParentTable = oRange.Tables(1)
End If

Set oSelectionTable = Nothing
Set oRange = Nothing

End Function




Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
F

Frenchy

Thank you,

i think your code will be helpfull

Shauna Kelly said:
Hi Frenchy

I used the code below for something similar a while ago. I seem to remember
that it worked reliably, although I haven't tested it recently. I hope it's
useful.


Function GetParentTable(nLevel As Integer) As Table
'Author: Shauna Kelly, February 2003
'Purpose: Return reference to a parent table at nesting
'level nLevel. 'If nLevel = 1, then this returns a reference
'to the highest level of table in which the selection resides.
'If the selection is not within a table, this returns Nothing.
'Note that the selection is not changed by this function.

Dim oSelectionTable As Table
Dim nNestingLevel As Integer
Dim oRange As Range

If Selection.Information(wdWithInTable) Then
'Get the current table
Set oSelectionTable = Selection.Tables(1)

'Capture the current table's nesting level and set a
'range to start working with.
With oSelectionTable
nNestingLevel = .NestingLevel
Set oRange = .Range.Cells(1).Range.Characters(1)
End With

'Keep moving the range one character back until the
'character's table's nesting level reaches your required
'level
Do While nNestingLevel > nLevel
oRange.MoveStart Unit:=wdCharacter, Count:=-1
nNestingLevel = oRange.Tables(1).NestingLevel
Loop

'Set return value
Set GetParentTable = oRange.Tables(1)
End If

Set oSelectionTable = Nothing
Set oRange = Nothing

End Function




Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word


Frenchy said:
Hello, I'm developping an add-in with C# .net

I'd like to select the parent table of a specific table. i've found the
NestingLevel property, but It's read-only.

i tried to use an other property: Parent
But it gives an object and I can't use It as a table:

Word.Table mytbl =
this.appWord.ActiveDocument.ActiveWindow.Selection.Range.Tables[1];

Word.Table tbltest = (Word.Table)mytbl.Parent;

^->doesn't work.

So...I get the first table selected by the user but I'd like to get the
parent table. how could i do?


Thanks,

Frenchy
 
F

Frenchy

And here's what it looks like in c#:

rangeSelect.Start=rangeSelect.Tables[1].Range.Start;
object unit = Word.WdUnits.wdCharacter;
object nb = -1;
while(rangeSelect.Tables[1].NestingLevel!=1)
{
rangeSelect.MoveStart(ref unit,ref nb);
}
unit = Word.WdUnits.wdTable;
rangeSelect.MoveStart(ref unit,ref nb);

thanks again,

Frenchy

Frenchy said:
Thank you,

i think your code will be helpfull

Shauna Kelly said:
Hi Frenchy

I used the code below for something similar a while ago. I seem to remember
that it worked reliably, although I haven't tested it recently. I hope it's
useful.


Function GetParentTable(nLevel As Integer) As Table
'Author: Shauna Kelly, February 2003
'Purpose: Return reference to a parent table at nesting
'level nLevel. 'If nLevel = 1, then this returns a reference
'to the highest level of table in which the selection resides.
'If the selection is not within a table, this returns Nothing.
'Note that the selection is not changed by this function.

Dim oSelectionTable As Table
Dim nNestingLevel As Integer
Dim oRange As Range

If Selection.Information(wdWithInTable) Then
'Get the current table
Set oSelectionTable = Selection.Tables(1)

'Capture the current table's nesting level and set a
'range to start working with.
With oSelectionTable
nNestingLevel = .NestingLevel
Set oRange = .Range.Cells(1).Range.Characters(1)
End With

'Keep moving the range one character back until the
'character's table's nesting level reaches your required
'level
Do While nNestingLevel > nLevel
oRange.MoveStart Unit:=wdCharacter, Count:=-1
nNestingLevel = oRange.Tables(1).NestingLevel
Loop

'Set return value
Set GetParentTable = oRange.Tables(1)
End If

Set oSelectionTable = Nothing
Set oRange = Nothing

End Function




Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word


Frenchy said:
Hello, I'm developping an add-in with C# .net

I'd like to select the parent table of a specific table. i've found the
NestingLevel property, but It's read-only.

i tried to use an other property: Parent
But it gives an object and I can't use It as a table:

Word.Table mytbl =
this.appWord.ActiveDocument.ActiveWindow.Selection.Range.Tables[1];

Word.Table tbltest = (Word.Table)mytbl.Parent;

^->doesn't work.

So...I get the first table selected by the user but I'd like to get the
parent table. how could i do?


Thanks,

Frenchy
 

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