williamr was telling us:
williamr nous racontait que :
I wouldlike to draw lines on a word document using VBA, something
like a table but using VBA, (Trying to teach myself) but don't know
where to start. Would someone show me, please??
To draw a line and to manipulate it declare a shape object and then use it
to create a line:
'_______________________________________
Dim shpLine As Shape
With ActiveDocument.Shapes
Set shpLine = .AddLine(135#, 135#, 450#, 135#)
With shpLine
With .Line
.BeginArrowheadStyle = msoArrowheadDiamond
.BeginArrowheadLength = msoArrowheadLong
.EndArrowheadStyle = msoArrowheadTriangle
.EndArrowheadLength = msoArrowheadLong
End With
End With
End With
'_______________________________________
This creates an horizontal line because the coordinates 135 and 135 are the
X and Y of the line starting point, then 450 and 135 are the coordinates of
the end of the lines. Since both set of coordinates share the same Y values,
the line is horizontal.
For a vertical line, make sure that the X values are the same:
.AddLine(135#, 180#, 135#, 378#)
For a diagonal line, all coordinates are different:
.AddLine(135#, 153#, 423#, 320#)
Getting the desired angle can be tricky...
The line will be anchored to the currently selected paragraph. If you want
to anchor the line to a particular paragraph, let's say the second one in
the document, do this:
Set shpLine = .AddLine(135#, 135#, 450#, 135#, _
ActiveDocument.Range.Paragraphs(2).Range)
Note that in all cases, the coordinates are offset to the anchor paragraph
(The first character position).
Now, you wrote about drawing lines, like a table... perhaps you did not
explain yourself correctly and you meant that you want to add a table to a
document.
I mean, you do not need to actually draw each table border. You just add a
table and then format it or fill it as you wish:
'_______________________________________
Dim tblTest As Table
With ActiveDocument.Tables
'Add a table with 4 rows and 5 colomns
Set tblTest = .Add(Selection.Range, 4, 5)
With tblTest
With .Borders
.OutsideLineStyle = wdLineStyleDouble
.OutsideLineWidth = wdLineWidth225pt
.OutsideColorIndex = wdBlue
.InsideLineStyle = wdLineStyleSingle
.InsideLineWidth = wdLineWidth050pt
.InsideColor = wdColorBlack
End With
With .Cell(1, 1).Range
.Text = "In row 1, this is the first cell."
.Font.Bold = True
.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
.Cell(2, 3).Range.Text = "In row 2, this is the third cell."
End With
End With
'_______________________________________
This table is added at the current cursor location.
If you need to specify a particular paragraph, replace
Set tblTest = .Add(Selection.Range, 4, 5)
by something like the line example above:
Set tblTest = .Add(ActiveDocument.Range.Paragraphs(2).Range, 4, 5)
This will actually replace the second paragraph.
If you want to keep it and have the table immediately following the second
paragraph, you need something like:
'_______________________________________
Dim tblTest As Table
Dim rgeTable As Range
Set rgeTable = ActiveDocument.Range.Paragraphs(3).Range
rgeTable.Collapse wdCollapseStart
With ActiveDocument.Tables
Set tblTest = .Add(rgeTable, 4, 5)
With tblTest
With .Borders
.OutsideLineStyle = wdLineStyleDouble
.OutsideLineWidth = wdLineWidth225pt
.OutsideColorIndex = wdBlue
.InsideLineStyle = wdLineStyleSingle
.InsideLineWidth = wdLineWidth050pt
.InsideColor = wdColorBlack
End With
With .Cell(1, 1).Range
.Text = "In row 1, this is the first cell."
.Font.Bold = True
.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
.Cell(2, 3).Range.Text = "In row 2, this is the third cell."
End With
End With
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org