In Word 97, I have the following code which takes the selected rows from a
userform and inserts the data in table format into the Word document.

Ideally, I would like the table to be indented at 2.5cm (that is, the
left-hand border of column 1 is positioned at 2.5cm).

What do I need to include into the code to position the table at that
location?

Thank you for any suggestions.



Private Sub CommandButton1_Click()
Dim x As Long, y As Long, tbl As Table, n As Integer
x = ListBox1.ColumnCount + 1
' Count the number of selected records for creation of the table
n = 1
For y = 2 To ListBox1.ListCount + 1
If ListBox1.Selected(y - 2) = True Then
n = n + 1
End If
Next y

Selection.TypeParagraph
Set tbl = ActiveDocument.Tables.Add(Selection.Range, n, x)
tbl.Range.Borders.Enable = False
Dim tblcell As Cell
For Each tblcell In tbl.Range.Cells
tblcell.Borders.Enable = False
Next tblcell
tbl.Columns(1).Width = CentimetersToPoints(2.7)
tbl.Columns(2).Width = CentimetersToPoints(4.75)
tbl.Columns(3).Width = CentimetersToPoints(3.25)
tbl.Columns(4).Width = CentimetersToPoints(3.25)
tbl.Columns(5).Width = CentimetersToPoints(3.25)
Dim rg As Range
tbl.Columns(4).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
tbl.Columns(5).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Dim rgStart As Range, rgEnd As Range
tbl.Range.Font.Size = 11
tbl.Range.Font.Name = "Times New Roman"
tbl.Range.ParagraphFormat.SpaceBefore = 2
tbl.Range.ParagraphFormat.SpaceAfter = 2
tbl.Range.ParagraphFormat.LineSpacing = 12

Re: VBA - code for border for a table by Greg

Greg
Mon Nov 19 21:40:10 PST 2007

MIrving,

Not sure exactly how it would be done using cm, but something like this:
tbl.Rows.LeftIndent = InchesToPoints(0.3)



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


MIrving wrote:
> In Word 97, I have the following code which takes the selected rows
> from a userform and inserts the data in table format into the Word
> document.
>
> Ideally, I would like the table to be indented at 2.5cm (that is, the
> left-hand border of column 1 is positioned at 2.5cm).
>
> What do I need to include into the code to position the table at that
> location?
>
> Thank you for any suggestions.
>
>
>
> Private Sub CommandButton1_Click()
> Dim x As Long, y As Long, tbl As Table, n As Integer
> x = ListBox1.ColumnCount + 1
> ' Count the number of selected records for creation of the table
> n = 1
> For y = 2 To ListBox1.ListCount + 1
> If ListBox1.Selected(y - 2) = True Then
> n = n + 1
> End If
> Next y
>
> Selection.TypeParagraph
> Set tbl = ActiveDocument.Tables.Add(Selection.Range, n, x)
> tbl.Range.Borders.Enable = False
> Dim tblcell As Cell
> For Each tblcell In tbl.Range.Cells
> tblcell.Borders.Enable = False
> Next tblcell
> tbl.Columns(1).Width = CentimetersToPoints(2.7)
> tbl.Columns(2).Width = CentimetersToPoints(4.75)
> tbl.Columns(3).Width = CentimetersToPoints(3.25)
> tbl.Columns(4).Width = CentimetersToPoints(3.25)
> tbl.Columns(5).Width = CentimetersToPoints(3.25)
> Dim rg As Range
> tbl.Columns(4).Select
> Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
> tbl.Columns(5).Select
> Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
> Dim rgStart As Range, rgEnd As Range
> tbl.Range.Font.Size = 11
> tbl.Range.Font.Name = "Times New Roman"
> tbl.Range.ParagraphFormat.SpaceBefore = 2
> tbl.Range.ParagraphFormat.SpaceAfter = 2
> tbl.Range.ParagraphFormat.LineSpacing = 12



Re: VBA - code for border for a table by MIrving

MIrving
Mon Nov 19 22:29:00 PST 2007

Thank you Greg. I changed Inches to Centimetres and it worked perfectly.
Much appreciated.

"Greg Maxey" wrote:

> MIrving,
>
> Not sure exactly how it would be done using cm, but something like this:
> tbl.Rows.LeftIndent = InchesToPoints(0.3)
>
>
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
>
> MIrving wrote:
> > In Word 97, I have the following code which takes the selected rows
> > from a userform and inserts the data in table format into the Word
> > document.
> >
> > Ideally, I would like the table to be indented at 2.5cm (that is, the
> > left-hand border of column 1 is positioned at 2.5cm).
> >
> > What do I need to include into the code to position the table at that
> > location?
> >
> > Thank you for any suggestions.
> >
> >
> >
> > Private Sub CommandButton1_Click()
> > Dim x As Long, y As Long, tbl As Table, n As Integer
> > x = ListBox1.ColumnCount + 1
> > ' Count the number of selected records for creation of the table
> > n = 1
> > For y = 2 To ListBox1.ListCount + 1
> > If ListBox1.Selected(y - 2) = True Then
> > n = n + 1
> > End If
> > Next y
> >
> > Selection.TypeParagraph
> > Set tbl = ActiveDocument.Tables.Add(Selection.Range, n, x)
> > tbl.Range.Borders.Enable = False
> > Dim tblcell As Cell
> > For Each tblcell In tbl.Range.Cells
> > tblcell.Borders.Enable = False
> > Next tblcell
> > tbl.Columns(1).Width = CentimetersToPoints(2.7)
> > tbl.Columns(2).Width = CentimetersToPoints(4.75)
> > tbl.Columns(3).Width = CentimetersToPoints(3.25)
> > tbl.Columns(4).Width = CentimetersToPoints(3.25)
> > tbl.Columns(5).Width = CentimetersToPoints(3.25)
> > Dim rg As Range
> > tbl.Columns(4).Select
> > Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
> > tbl.Columns(5).Select
> > Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
> > Dim rgStart As Range, rgEnd As Range
> > tbl.Range.Font.Size = 11
> > tbl.Range.Font.Name = "Times New Roman"
> > tbl.Range.ParagraphFormat.SpaceBefore = 2
> > tbl.Range.ParagraphFormat.SpaceAfter = 2
> > tbl.Range.ParagraphFormat.LineSpacing = 12
>
>
>