Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting the current table 2

Status
Not open for further replies.

swingkyd

Programmer
Jul 10, 2003
34
CA
I'm trying to play around with setting up a table macro.

Any help would be greatly appreciated...I've spent hours on this problem and can't seem to solve it!

The goal is to ask the user the rows, columns, (done) and then format to a standard formatting template. (can't get working) Following this I wanted the User to enter the caption information in the caption.

As it stands I have some very BAD code that sort of does it... what I can't figure out is how to set a range for the table created.
1) How the formatting should loook:
The new table has a different 'style' from the rest of the text. The header should have style:'Header Text' and the rest of the cells should have the style: 'Table Text'.

I wanted to create a range of all the cells after the header and format it as 'Table Text' (this is what I've set up recursively). I'd rather set this up using the range and then apply the style I want for this range.

2) What I can't get:
I need to select the table without knowing the index of the table. I can't seem to figure this out. I wanted to set the various properties of the table but I have to know the 'index as a Long'. I don't know this number since the user will use the code to set up any number of tables, anywhere in the text.

3) Here's the code so far:
Dim enter_Rows As Integer
Dim enter_Columns As Integer
Dim count As Integer

Set aDoc = ActiveDocument
Code:
' function to get the number of rows and columns
Get_Rows_Columns Rows:=enter_Rows, Columns:=enter_Columns
    
Set New_Table = aDoc.Tables.Add( _
    Range:=Selection.Range, _
    NumRows:=enter_Rows, _
    NumColumns:=enter_Columns, _
    DefaultTableBehavior:=wdWord9TableBehavior, _
    AutoFitBehavior:=wdAutoFitFixed)

If enter_Rows > 1 Then  ' if greater than 1 that is
    Selection.SelectRow
    Selection.Style = aDoc.Styles("Table Header")
    
    For count = 1 To (enter_Rows - 1)
    Selection.MoveDown Unit:=wdLine, count:=1
    Selection.SelectRow
    Selection.Style = ActiveDocument.Styles("Table Text")
    Next count

' With New_Table =
'    Set Row_Range = aDoc.New_Table.Rows
'    With Row_Range
End If
    Selection.Move Unit:=wdRow, count:=-enter_Row ' this does not work
[\code]
 
Hey swing,

If you want to select the very LAST table then...
Code:
ActiveDocument.Tables(ActiveDocument.Tables.count).Select
What is that last statement supposed to do? How does that relate to your question about selecting the table?

???

Skip,
Skip@TheOfficeExperts.com
 
thanks for responding...
it's not just the last table I need selected. If the user is making a big report...then wants to add a new table somewhere in the middle, selecting the last one won't work. if the selection is somewhere in the table some command should let it select the current table.

I am hoping to be able to select the table so I can create a range to apply formatting (say to the heading...then to the rows below).

See how the following code is recursive? I wanted to know how to create a the two ranges in question by selecting a set of rows.
Code:
If enter_Rows > 1 Then  ' if greater than 1 that is
    Selection.SelectRow
    Selection.Style = aDoc.Styles("Table Header")
    
    For count = 1 To (enter_Rows - 1)
    Selection.MoveDown Unit:=wdLine, count:=1
    Selection.SelectRow
    Selection.Style = aDoc.Styles("Table Text")
    Next count
[\code]

There doens't seem to be a way to just grab the current selection. I was hoping to use code something like the following to apply things like "centering the table at 90% of the margin":
[code]
    With Selection.Tables(1).Rows
        .WrapAroundText = True
        .HorizontalPosition = wdTableCenter
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
        .DistanceLeft = InchesToPoints(0.13)
        .DistanceRight = InchesToPoints(0.13)
        .VerticalPosition = InchesToPoints(0)
        .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
        .DistanceTop = InchesToPoints(0)
        .DistanceBottom = InchesToPoints(0)
        .AllowOverlap = False
    End With
    Selection.Tables(1).PreferredWidthType = wdPreferredWidthPercent
    Selection.Tables(1).PreferredWidth = 90
[\code]
See how the table requires an index? what if you don't know that index... or just want the current table? What if you only wanted to select certain rows...
See how to change the "preferred width type" you need to hav the "Long" index of the table instead of just saying the present table


How are the ranges that I talk about supposed to be created?
Can you see what I'm doing or should I try to clarify some more? (I can be wordy sometimes...LOL) 

does this make any sense?
 
Hi swingkyd,

After you insert a table, the Selection is in the Table and there is only one table in the Selection so Selection.Tables(1) will establish a reference to it. What I sometimes do is ...

Code:
Dim ActiveTable As Table
Selection.Tables.Add Selection.Range, 5, 5
Set ActiveTable = Selection.Tables(1)

... after which you can reference the table whenever you want without knowing where it is. To work with rows in the table you can use the Rows Collection, either like this ...

Code:
Dim TableRow As Row
For Each TableRow in ActiveTable.Rows
    TableRow.Range.Style = ActiveDocument.Styles("Table Text")
Next

... or like this ...

Code:
ActiveTable.Rows(1).Range.Style = ActiveDocument.Styles("Table Header")

Enjoy,
Tony
 
Well working with the current table is not a problem. What is a problem for you is a previously created table. You need some means of determining which one to reference using a relative index from where you are, OR a table name if you have given your table names.

Skip,
Skip@TheOfficeExperts.com
 
Okay... with some massaging, this is what I've come up with.

a solution...

Thanks TonyJollans!!!
However, your for loop didn't work but your selection declaration made the whole thing work. I just created a For loop with the index in the Rows(i) to apply the "Table Text" style. All worked well once I thought about it over the weekend.

SkipVought: I'm not too sure what you are getting at. However, you might have a point now that I'm thinking about it. What if I want to apply these formats to any table. Let's say I get information imported from a LaTeX document that's in RTF... Can I just select the table and run a macro something like "AutoFormat"...

Will this selection method work the same way if someone is already "inside" a table imported from RTF? If so, then I might be able to reuse this code. Will the Auto-Captioning work?

 
TonyJollans:
To further clarify: I didn't understand what the middle line with the "Range, 5, 5" did??
Code:
Dim ActiveTable As Table
Selection.Tables.Add Selection.Range, 5, 5
Set ActiveTable = Selection.Tables(1)
[\code]

The compiler error was that the "with" statement or object was not defined or something like that:
[code]
Dim TableRow As Row
For Each TableRow in ActiveTable.Rows
    TableRow.Range.Style = ActiveDocument.Styles("Table Text")
Next
[\code]

It didn't think that there was a ".Range.Style" property to the TableRow declaration. Isn't the "Row" a variant? How does Word know how this variant behaves?

Here's what I used:
[code]
Set NewTable = Selection.Tables(1)

With NewTable
    .PreferredWidthType = wdPreferredWidthPercent
    .PreferredWidth = 90
    .Range.Style = aDoc.Styles("Table Text")
End With

With NewTable.Rows
    .WrapAroundText = True
    .HorizontalPosition = wdTableCenter
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
    .DistanceLeft = InchesToPoints(0.13)
    .DistanceRight = InchesToPoints(0.13)
    .VerticalPosition = InchesToPoints(0)
    .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
    .DistanceTop = InchesToPoints(0)
    .DistanceBottom = InchesToPoints(0)
    .AllowOverlap = False
End With

If enter_Rows >= 2 Then
    With NewTable.Rows(1)
        .HeadingFormat = True
        .Range.Style = aDoc.Styles("Table Header")
    End With
End If
[\code]

works like a charm!!!
 
Hi swingkyd,

Glad you've got it all working. To answer your questions ..

1. If you are inside a table it shouldn't matter where the table came from - it's a table and that's that, so Selection.Tables(1) should give you a reference to the whole table. And no reason not to reuse the code.

2. "Selection.Range, 5, 5" on the Tables.Add is simply using positional parameters instead of named ones and is shorthand for "Range:=Selection.Range, NumRows:=enter_Rows, NumColumns:=enter_Columns"

3. Sorry I can't see what the problem might have been with the .Range.Style. Row is an Object and it has a Range property which returns a Range Object. Table.Rows is a collection of Row Objects.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top