W97: Macro to create table (row x column) specified by user 
Author Message
 W97: Macro to create table (row x column) specified by user

I know Word 97 is capable of this but can't figure it out. I need some
help in creating a macro to do the following in this order:

1. asks a user to specify the number of rows and columns
2. create the table with the values from step 1
3. put a blank row above and below the table
4. put another row at the top for the caption (i.e. Table 1: XYZ) with
a double bottom border
5. set the shading in the blank rows and user specified rows to 20%
Gray every other row.

The table should look like this if user specified a 3 rows x 7
columns:
+----------------------------+
| Table 1: XYZ               +
+=========================================+
|                                         | <-- 20% Gray shading
+-----+-----+-----+-----+-----+-----+-----+
+  1  |  2  |  3  |  4  |  5  |  6  |  7  | <-- White
+-----+-----+-----+-----+-----+-----+-----+
+  1  |  2  |  3  |  4  |  5  |  6  |  7  | <-- 20% Gray shading
+-----+-----+-----+-----+-----+-----+-----+
+  1  |  2  |  3  |  4  |  5  |  6  |  7  | <-- White
+-----+-----+-----+-----+-----+-----+-----+
|                                         | <-- 20% Gray shading
+-----------------------------------------+

Any help would be greatly appreciated.



Sat, 15 Oct 2005 22:23:36 GMT  
 W97: Macro to create table (row x column) specified by user
A tip if you write VBA to make tables is to make the rows and columns have
the same number throughout.   So, if it were allowed then what I would do
is to create three tables:

Table 1 contains one row and one column with the same width as...

Table 2 which is separated from the above by a 2pt paragraph.   This has
the grid like which you request.

Then another 2pt paragraph with one row.

If this isn't acceptable then I would make a table with x columns and y+2
rows.   Start populating the values in rows 2 onwards to y+1 inclusive.  
Then I would merge all the cells in rows 1 and y+2.

That would do it, I think.

Need some code, or is this enough of a head start to get you going?
  Malc
  www.dragondrop.com



Sat, 15 Oct 2005 22:52:00 GMT  
 W97: Macro to create table (row x column) specified by user
Malc,
Thanks for replying. I think I need a little more direction than that.
I am new to VBA Macros so some sample code would be nice to get
started with. After I wrote the message I started thinking about the
order of what I want the macro to do.

Instead of doing all those things I originally specified, how would I
accomplish the following:
1. Create a four row, 1 column table
2. Insert a caption (i.e. Table 1: XYZ) into row 1
3. Set the shading for row 2 to 20% Gray and the top border to a
double-line
4. Ask the user for the number of rows and columns
5. Split row 3 into the user specified number (from step 4)
6. Apply the alternating 20% Gray and White shading from rows 3 on.

Would that be easier to accomplish in a macro?

Where and how would I start? Please provide some code examples if you
don't mind.

Thanks,
Greg

Quote:

> A tip if you write VBA to make tables is to make the rows and columns have
> the same number throughout.   So, if it were allowed then what I would do
> is to create three tables:

> Table 1 contains one row and one column with the same width as...

> Table 2 which is separated from the above by a 2pt paragraph.   This has
> the grid like which you request.

> Then another 2pt paragraph with one row.

> If this isn't acceptable then I would make a table with x columns and y+2
> rows.   Start populating the values in rows 2 onwards to y+1 inclusive.  
> Then I would merge all the cells in rows 1 and y+2.

> That would do it, I think.

> Need some code, or is this enough of a head start to get you going?
>   Malc
>   www.dragondrop.com



Sun, 16 Oct 2005 22:02:32 GMT  
 W97: Macro to create table (row x column) specified by user
Greg

Okay, make a user form and on it make three textbox controls,
txtRows, txtColumns and txtCaption.   Also make a command button called
cmdOK and then make the following code.

Call this user form 'frmTables' then make this code in a module, which
creates the form at run time.  (If you are not used to making user forms
in VBA then go to my site and there you will see a tutorial on how to do
this).

Public Sub InsertTable()

  Dim ofrmTables As frmTables

  Set ofrmTables = New frmTables
  ofrmTables.Show
  '--------------
  Unload ofrmTables
  Set ofrmTables = Nothing

End Sub

What follows next is the code which goes behind the cmdOK button click
event:

Private Sub cmdOK_Click()

  ' Written by Malcolm Smith, www.dragondrop.com

  Dim nRows As Long
  Dim nColumns As Long

  Dim oTable As Table
  Dim nRow As Long
  Dim nColumn As Long

  Const SHADE As Long = wdColorGray20

  nRows = Val(Me.txtRows.Text)
  nColumns = Val(Me.txtColumns.Text)

  ' Check for valid user input
  If nColumns <= 0 Or nRows <= 0 Then
    MsgBox "Please enter positive row and column values.", _
        vbOKOnly, "Error: Data Entry"
    Exit Sub
  End If

  nRows = nRows + 3

  ' Create the new table
  Selection.Tables.Add Selection.Range, nRows, nColumns

  ' Set pointer to new table
  Set oTable = Selection.Tables(1)

  ' Make the central squares a chequer-board effect.
  For nRow = 3 To nRows - 1
    For nColumn = 1 To nColumns

      If nRow Mod 2 = 0 Then
        If nColumn Mod 2 = 0 Then
          oTable.Cell(nRow, nColumn).Shading.BackgroundPatternColor =
SHADE
        End If
      Else
        If nColumn Mod 2 = 1 Then
          oTable.Cell(nRow, nColumn).Shading.BackgroundPatternColor =
SHADE
        End If
      End If

    Next nColumn
  Next nRow

  ' The first, second and last rows have the cells merged together.
  oTable.Rows(1).Cells.Merge
  oTable.Rows(2).Cells.Merge
  oTable.Rows(nRows).Cells.Merge

  ' Insert caption
  oTable.Rows(1).Range.Text = txtCaption.Text
  oTable.Rows(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

  ' Set the shade and the border for the second row
  oTable.Rows(2).Range.Shading.BackgroundPatternColor = SHADE
  oTable.Rows(2).Range.Borders(wdBorderTop).LineStyle = wdLineStyleDouble

  Me.Hide

End Sub

If you have any questions then please shout.

Malc
  www.dragondrop.com



Mon, 17 Oct 2005 01:57:00 GMT  
 W97: Macro to create table (row x column) specified by user
Feedback?

Malc
 www.dragondrop.com



Mon, 17 Oct 2005 16:50:00 GMT  
 W97: Macro to create table (row x column) specified by user
Malc,
Thanks for your help. I followed your sample to the T but it keeps
{*filter*}. When I switch to VBA I get the following error message:

Compile Error:
Constant Expression Required.

The line 'Const SHADE As Long = wdColorGray20' is shown with
'wdColorGray20' highlighted.

What does that mean?

Quote:

> Feedback?

> Malc
>  www.dragondrop.com



Tue, 18 Oct 2005 04:48:27 GMT  
 W97: Macro to create table (row x column) specified by user
Greg

Odd.  Ok, go to the IDE and then open the immediate window and then enter
the command:

? wdColorGray20

It will return a value.  Replace that string in the Const statement with
that number and then see if it works.

Malc
  www.ukhorseracing.co.uk



Tue, 18 Oct 2005 17:07:00 GMT  
 W97: Macro to create table (row x column) specified by user
Malcolm,
First of all, thank you for your continued help.

Forgive me for being stupid but I am really new to VBA programming in
Word. What is the IDE? Is that Visual Basic Editor window?

Also I open an Immediate window and typed the command and then pressed
ENTER to get the value. Nothing happened, the cursor just went to the
next line.

What now?

Again, thanks.
Greg

Quote:

> Greg

> Odd.  Ok, go to the IDE and then open the immediate window and then enter
> the command:

> ? wdColorGray20

> It will return a value.  Replace that string in the Const statement with
> that number and then see if it works.

> Malc
>   www.ukhorseracing.co.uk



Tue, 18 Oct 2005 21:20:50 GMT  
 W97: Macro to create table (row x column) specified by user
Yes, the IDE is the VBA editor.

In the immediate window (control-g) and you enter:

? wdColorGray20

and then you hit enter and nothing appears then I would suggest that
something is odd with the Word installation.  

Anyone else any ideas?

Malc
  www.dragondrop.com



Tue, 18 Oct 2005 21:30:00 GMT  
 W97: Macro to create table (row x column) specified by user
Greg,

If you type
? wdColor
in the immediate window and a dot thereafter, does VBA dropdown/intellisense
any other color you can choose from?

PdL



Quote:
> Yes, the IDE is the VBA editor.

> In the immediate window (control-g) and you enter:

> ? wdColorGray20

> and then you hit enter and nothing appears then I would suggest that
> something is odd with the Word installation.

> Anyone else any ideas?

> Malc
>   www.dragondrop.com



Tue, 18 Oct 2005 21:31:31 GMT  
 W97: Macro to create table (row x column) specified by user
Perry and Malcolm,
The wdColorGray20 is not valid in the Word 97 version I am using. I
can only choose from wdGray25 and wdGray50.

Any occurrences of

BackgroundPatternColor

caused the macro to return error messages, but if I changed it to

BackgroundPatternColorIndex

everything works

The only other issues are
1. I don't want the rows and columns specified by the user to be a
checkerboard effect. I need the first row to be gray, the next row to
be white, the third row to be gray, etc. to the last row they
specified. (Look in http:\\photos.yahoo.com\gdavisds\Example of Table
Format)

2. The rows they specified and the should have no borders and the row
above should have no bottom border and the last row should have no top
border.

3. I want the caption to have the style I defined as Table Caption.

How would I go about changing these?

Again, you have been a great help. I don't know where I would've
started with this without your guidance. Thanks again.

Greg

Quote:

> Greg,

> If you type
> ? wdColor
> in the immediate window and a dot thereafter, does VBA dropdown/intellisense
> any other color you can choose from?

> PdL



> > Yes, the IDE is the VBA editor.

> > In the immediate window (control-g) and you enter:

> > ? wdColorGray20

> > and then you hit enter and nothing appears then I would suggest that
> > something is odd with the Word installation.

> > Anyone else any ideas?

> > Malc
> >   www.dragondrop.com



Wed, 19 Oct 2005 01:48:52 GMT  
 W97: Macro to create table (row x column) specified by user


Quote:
> Malc,
> Thanks for your help. I followed your sample to the T but it keeps
>{*filter*}. When I switch to VBA I get the following error message:

> Compile Error:
> Constant Expression Required.

> The line 'Const SHADE As Long = wdColorGray20' is shown with
> 'wdColorGray20' highlighted.

> What does that mean?

It means that Malcolm's code was written using Word 2000 or later, and you
are running Word 97. In Word 97, there are a very limited set of colors
(only 16 of them), and 20% grey is not one of them. Fortunately there is a
workaround, in that table shading can be applied as a texture, being a
percentage mix of two of the limited number of available solid colors.

Here is Malcolm's code, reworked so that it should work in Word 97. I don't
have a Word 97 installation handy at the moment, but I'm fairly confident it
should run OK.

Private Sub cmdOK_Click()

  ' Written by Malcolm Smith, www.dragondrop.com
  ' Modified for Word 97 by Jonathan West

  Dim nRows As Long
  Dim nColumns As Long

  Dim oTable As Table
  Dim nRow As Long
  Dim nColumn As Long

  nRows = Val(Me.txtRows.Text)
  nColumns = Val(Me.txtColumns.Text)

  ' Check for valid user input
  If nColumns <= 0 Or nRows <= 0 Then
    MsgBox "Please enter positive row and column values.", _
        vbOKOnly, "Error: Data Entry"
    Exit Sub
  End If

  nRows = nRows + 3

  ' Create the new table
  Selection.Tables.Add Selection.Range, nRows, nColumns

  ' Set pointer to new table
  Set oTable = Selection.Tables(1)

  ' Make the central squares a chequer-board effect.
  For nRow = 3 To nRows - 1
    For nColumn = 1 To nColumns
      If (nRow + nColumn) Mod 2 = 0 Then
         With oTable.Cell(nRow, nColumn).Shading
           .BackgroundPatternColorIndex = wdWhite
           .ForegroundPatternColorIndex = wdBlack
           .Texture = wdTexture20Percent
         End With
      End If

    Next nColumn
  Next nRow

  ' The first, second and last rows have the cells merged together.
  oTable.Rows(1).Cells.Merge
  oTable.Rows(2).Cells.Merge
  oTable.Rows(nRows).Cells.Merge

  ' Insert caption
  oTable.Rows(1).Range.Text = txtCaption.Text
  oTable.Rows(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

  ' Set the shade and the border for the second row
  With oTable.Rows(2).Range.Shading
   .BackgroundPatternColorIndex = wdWhite
   .ForegroundPatternColorIndex = wdBlack
   .Texture = wdTexture20Percent
  End With

  oTable.Rows(2).Range.Borders(wdBorderTop).LineStyle = wdLineStyleDouble

  Me.Hide

End Sub

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.*-*-*.com/
Word FAQs at http://www.*-*-*.com/
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email



Wed, 19 Oct 2005 03:37:23 GMT  
 W97: Macro to create table (row x column) specified by user
Jonathon

I do like your:

Quote:
>     If (nRow + nColumn) Mod 2 = 0 Then

Yes, good thinking there!

Cheers
  Malc
  www.dragondrop.com



Wed, 19 Oct 2005 03:51:00 GMT  
 W97: Macro to create table (row x column) specified by user
That worked great! Thanks, Jonathon and Malcolm.

The shading is 20% Gray like I need.

However there are the only other issues I mentioned in my last post:
1. I don't want the rows and columns specified by the user to be a
checkerboard effect. I need the first row to be gray, the next row to
be white, the third row to be gray, etc. to the last row they
specified. (Look in http:\\photos.yahoo.com\gdavisds\Example of Table
Format)

2. The rows they specified and the should have no borders and the row
above should have no bottom border and the last row should have no top
border.

3. I want the caption to have the style I defined as Table Caption.

How would I go about changing these?

Again, thanks for all of your help.

Greg

Quote:

> Jonathon

> I do like your:

> >     If (nRow + nColumn) Mod 2 = 0 Then

> Yes, good thinking there!

> Cheers
>   Malc
>   www.dragondrop.com



Fri, 21 Oct 2005 20:27:11 GMT  
 W97: Macro to create table (row x column) specified by user
Goodness; let me have a play later on.

Malc
  www.ukhorseracing.co.uk



Fri, 21 Oct 2005 21:55:00 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. How to Create 2 column table and continue adding rows

2. Error creating rows with columns having different column width

3. Insert number of rows specified by user?

4. macro to create specified number of labels

5. Porting a VBA-macro from W97 to W2k

6. W97 macro warnings w/ embedded documents

7. Creating distribution disk - want to have user specify directory for the data

8. Specifying field size when using CREATE TABLE

9. Normalizing non-normal tables already containing data;OR changing columns into rows

10. Running a macro to add a row in a table

11. Getting row/column index of HTML TABLE cell

12. How using ADO Create a Table and Specify that a Field is autoNumber and Primary Key

 

 
Powered by phpBB® Forum Software