Home |
Search |
Today's Posts |
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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 | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Table headers/footers and layout | Page Layout | |||
How to Avoid Word 2003 Table Style Problems | Tables | |||
Table AutoFormats vs. Table Styles confusion | Tables | |||
Select Table dialog box comes up twice when open data source | Mailmerge | |||
How do I select specific information from an imported table | Tables |