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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Word table and then format it 3

Status
Not open for further replies.

sophisticatedpenguin

Technical User
Oct 12, 2005
31
GB
Hi,

I'm trying to write a macro to insert a table in Word based on user input for the no. of rows and columns, and then to format it in a certain way.

Below is my code so far (apologies if it's not very good, I'm not experienced at this!) I set up my own table style called "MWGtable" and I want to apply that to the whole table. Will what I have apply the formatting to the entire table that has just been inserted? If not, how do I get it to select the whole table, please?

Many thanks!
Katie

Dim rownumber As Long
Dim colnumber As Long

rownumber = InputBox("How many rows?")
colnumber = InputBox("How many columns?")

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=rownumber, NumColumns:=colnumber, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "MWTtable" Then
.Style = "MWGtable"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True

End With
 
I'd replace this:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=rownumber, NumColumns:=colnumber, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)

with this:
Dim objTable As Word.Table
Set objTable = ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=rownumber, NumColumns:=colnumber, AutoFitBehavior:=wdAutoFitFixed
With objTable

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

Thanks very much for your reply.

VBA is throwing up an error with the line beginning

Set objTable = ...

Please could you advise?

Many thanks again for your assistance.
Katie
 



Hi,

Need parentheses
Code:
Dim objTable As Word.Table, rownumber As Integer, colnumber As Integer
rownumber = 4
colnumber = 4
Set objTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=rownumber, NumColumns:=colnumber, AutoFitBehavior:=wdAutoFitFixed)

With objTable

End With

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks guys! Now I just need to persuade Word to 'listen' to my table style properly (but that's another story – and another forum!)

Many thanks again,
Katie
 
<teasing>was that a real typo, or did you simply forget to put in the parenthesis?</teasing>

Regarding tables styles, due warning is required. They do not work as well as one would hope. But in any case, if you have a whole table style...
Code:
Dim rownumber As Long
Dim colnumber As Long
Dim otable As Table

rownumber = InputBox("How many rows?")
colnumber = InputBox("How many columns?")
Set otable = ActiveDocument.Tables.Add _
      (Range:=Selection.Range, NumRows:=rownumber, _
      NumColumns:=colnumber, _
      AutoFitBehavior:=wdAutoFitFixed)

otable.Range.Style = "MyWholeTable"
Note that if you have a separate HeaderRow table style, and it is different from the Whole Table style (often the case)...you can NOT use both. One or the other. In which case, create a paragraph style that you can apply to the header row, and then use that. Like so:
Code:
Dim rownumber As Long
Dim colnumber As Long
Dim otable As Table

rownumber = InputBox("How many rows?")
colnumber = InputBox("How many columns?")
Set otable = ActiveDocument.Tables.Add _
      (Range:=Selection.Range, NumRows:=rownumber, _
      NumColumns:=colnumber, _
      AutoFitBehavior:=wdAutoFitFixed)

With otable
   .Range.Style = "MyWholeTable"
   .Rows(1).Range.Style = "MyTableHeader"
End With

Gerry
 
Thanks very much Fumei. I still had trouble because the body of the table (ie everything apart from the header row) was still being overridden by the default para style. So I found out how to select every row except the header row, and applied another para style. I left the table style in because it does give the correct borders if nothing else!

Final code is below. I'm feeling way out of my depth but it SEEMS to work ...

I can't thank you all enough for your help and patience. It's very much appreciated. Tomorrow's job will be to find out how to make it exit gracefully if someone doesn't input sensible numbers :)

Katie

Code:
Dim rownumber As Long
Dim colnumber As Long
Dim oTable As Table

rownumber = InputBox("How many rows?")
colnumber = InputBox("How many columns?")

Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=rownumber, NumColumns:=colnumber, AutoFitBehavior:=wdAutoFitFixed)
With oTable
        .Range.Style = "MWGtable"
        .Rows(1).Range.Font.Bold = True
        .Rows(1).Range.Style = "MWGtableheader"
End With
    
oTable.Select
Selection.SetRange Start:=Selection.Rows(2).Range.Start, End:=Selection.End
With Selection
    .Range.Style = "MWGtabletext"
End With
 
You should not be doing two instructions like:
Code:
       .Rows(1).Range.Font.Bold = True 
       .Rows(1).Range.Style = "MWGtableheader"
As you are actioning the same thing - Rows(1) - the format characteristic (Bold) should be IN the Style MWGtableheader. Why is it not? If it is in the Style (as it should be) then:
Code:
       .Rows(1).Range.Style = "MWGtableheader"
should do it.

2. why are you selecting the table??????
Code:
With oTable
    .Range.Style = "MWGtable"
    .Rows(1).Range.Font.Bold = True
    .Rows(1).Range.Style = "MWGtableheader"
End With

oTable.Select
Selection.SetRange Start:=Selection.Rows(2).Range.Start, End:=Selection.End
With Selection   
   .Range.Style = "MWGtabletext"
End With
You already have the table object.


"Tomorrow's job will be to find out how to make it exit gracefully if someone doesn't input sensible numbers "

Indeed. That is error-trapping, and IMO is the hardest thing of all to do. Good solid error-trapping can take up to 50%+ of all the development time for a complicated piece of code.

First thing? Clearly, explicitly define "sensible numbers".

> 3,000 rows?
> 4,000 columns? (I certainly think so!)

What if they put in text? If they put in text, you will get a type mis-match - the dreaded error 13 - (as the result of your inputbox is a Long). This is easily handled with something like:
Code:
On Error GoTo MyErrorTrap
rownumber = InputBox("How many rows?")
colnumber = InputBox("How many columns?")

[COLOR=red]' the other stuff[/color red]

MyErrorTrap:
If Err.Number = 13 Then
   MsgBox "Hey...you entered text!  Use a number!"
End If
However, the limits of a numeric value - say 4,000 columns - is up to you. Reasonably easy though:
Code:
Dim rownumber As Long
Dim colnumber As Long
Dim oTable As Table

On Error GoTo MyErrorTrap
Start:
   rownumber = InputBox("How many rows?")
   colnumber = InputBox("How many columns?")
      If colnumber > 6 Then
         MsgBox "Whoa Nellie.  That is a LOT of columns."
         GoTo Start:
      End If
   
   Set oTable = ActiveDocument.Tables.Add _
      (Range:=Selection.Range, NumRows:=rownumber, _
      NumColumns:=colnumber, AutoFitBehavior:=wdAutoFitFixed)
   
   With oTable
      .Range.Style = "InsideTable"
      .Rows(1).Range.Style = "TableHeader"
   End With
   GoTo MyExit:
   
MyErrorTrap:
   If Err.Number = 13 Then
      MsgBox "Hey...you entered text!  Use a number!"
      GoTo Start:
   End If
If they enter text, it goes to MyErrorTrap and they will get the message about using text, and it returns to the start. If they put in more than 6 as the column count, they get a message about lots of columns, and it starts over.

NOTE: this is not solid error-trapping!. There are huge holes in this. I am just putting it out as a starting point.

Gerry
 
You should not be doing two instructions ..."

I tried just using the style, and the bold is built into the style, but for some reason it isn't getting applied – I have no idea why. So I put in the separate instruction as a pragmatic approach!

"why are you selecting the table??????"

Because I don't know how else to do something to all the rows except the first. (I'm the first to admit that I struggle with all of this.) Please could you advise a neater solution, and I will try to learn from it?

Thanks very much for the tips on error trapping :)

Katie
 
Code:
Dim oTable As Table
Dim oRow As Row
Set oTable = ActiveDocument.Tables(3)
For Each oRow In oTable.Rows
   If oRow.Cells(1).RowIndex <> 1 Then
       oRow.Range.Style = "Yadda"
       [COLOR=red]' or any other stuff to the row
       ' say oRow.Range.Bold = True[/color red]
   End If
Next
Every row that is NOT rowindex(1) is actioned. That is, every row except the first one (RowIndex = 1) is actioned.


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top