
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