Reply
 
Thread Tools Display Modes
  #1   Report Post  
Frenchy
 
Posts: n/a
Default How to select the parent table?

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
  #2   Report Post  
Shauna Kelly
 
Posts: n/a
Default

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" wrote in message
...
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




  #3   Report Post  
Frenchy
 
Posts: n/a
Default

Thank you,

i think your code will be helpfull

"Shauna Kelly" wrote:

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" wrote in message
...
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





  #4   Report Post  
Frenchy
 
Posts: n/a
Default

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" wrote:

Thank you,

i think your code will be helpfull

"Shauna Kelly" wrote:

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" wrote in message
...
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





Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Table headers/footers and layout Keith Page Layout 1 April 8th 05 07:37 PM
How to Avoid Word 2003 Table Style Problems Judy Haynes Tables 0 March 23rd 05 06:41 PM
Table AutoFormats vs. Table Styles confusion Tony Jollans Tables 5 March 6th 05 07:18 PM
Select Table dialog box comes up twice when open data source Paula Jern Mailmerge 0 January 6th 05 05:07 PM
How do I select specific information from an imported table smintey Tables 4 December 7th 04 07:54 PM


All times are GMT +1. The time now is 10:25 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"