Determine slope of line drawing or start/end coordinates

G

Greg Maxey

Yesterday in the docManagement group there was some discussion on formatting
line drawing object drawn with Word2007. When these objects are drawn using
the UI, the style attribute is disabled.

Lines drawn in Word2003 and pasted into Word2007 can be fully formatted
(style enabled) in the UI. Also lines created in Word2007 using VBA can be
fully formatted in the UI.

I have put together some code intended to duplicate a line drawn using the
UI as a line that can be fully formatted and delete the original. I draw
the line and run this code:

Sub Test()
Dim oShp As Word.Shape
Dim oShpNew As Word.Shape
Dim i, j, k, l As Long
Set oShp = Selection.ShapeRange(1)
i = oShp.Left
j = oShp.Top
k = oShp.Height
l = oShp.Width
oShp.Delete
Set oShpNew = ActiveDocument.Shapes.AddLine(i, j, i + 72, j)
With oShpNew
.Left = i
.Top = j
.Width = l
.Height = k
With .Line
.Style = msoLineThinThin
.Weight = 3
End With
End With
End Sub

It works great provided the line is vertical, horizontal or slopes left to
right down the page. Lines that slope rigth to left down the page appear as
a mirror image of the original line sloped left to right down the page. I
could easily fix this using flip, but I can't figure out how to tell (with
the procedure) if the line will need flipping.

Does anyone have any ideas how to determine the direction of slope or any
other idea for correctly duplicationg rigth to left sloping lines?

Thanks.


--
Greg Maxey

See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.

"It is not the critic who counts, not the man who points out how the strong
man stumbles, or where the doer of deeds could have done them better. The
credit belongs to the man in the arena, whose face is marred by dust and
sweat and blood, who strives valiantly...who knows the great enthusiasms,
the great devotions, who spends himself in a worthy cause, who at the best
knows in the end the triumph of high achievement, and who at the worst, if
he fails, at least fails while daring greatly, so that his place shall never
be with those cold and timid souls who have never known neither victory nor
defeat." - TR
 
G

George Lee

Did you ever get an answer?

The problem is that the line is defined by its bounding box, so there is no
lineStart or lineEnd per se. Instead:

Sub Test()
Dim oShp As Word.Shape
Dim oShpNew As Word.Shape
Dim i, j, k, l As Long

Debug.Print "i is of type: " & VarType(i)
Debug.Print "l is of type: " & VarType(l)

Dim theMargin As Long 'Page margin of the page. Needed to offset the
line properly.
theMargin = 36

Set oShp = Selection.ShapeRange(1)
i = oShp.Left + theMargin
j = oShp.Top + theMargin
k = oShp.Height
l = oShp.Width
oShp.Delete

Set oShpNew = ActiveDocument.Shapes.AddLine(i, k + j, l + i, j)
With oShpNew.Line
.Style = msoLineThinThin
.Weight = 3
End With
End Sub

There is also a minor mistake with the dim statement. Multiple values are
not assigned that way; only the last one is a true long. The two debug
statements show that they are different types.
 
T

Tony Jollans

Hi Greg,

Take a look at the HorizontalFlip property of the Shape - it should be True
for a line sloping top right to bottom left.
 
G

Greg Maxey

Tony,

Yes, that or vertical flip either one should work. However, that value
seems to be related to how the line was drawn to begin with. For example,
if I draw a line sloping top right to bottom left and I start the line at
the top then that does return true and this code works:

Sub DuplicateAndReplaceLine()
Dim oShp As Word.Shape
Dim bFlip As Boolean
Dim oShpNew As Word.Shape
Dim i As Long, j As Long, k As Long, l As Long
Set oShp = Selection.ShapeRange(1)
bFlip = oShp.HorizontalFlip
i = oShp.Left
j = oShp.Top
k = oShp.Height
l = oShp.Width
oShp.Delete
Set oShpNew = ActiveDocument.Shapes.AddLine(i, j, i + 72, j)
With oShpNew
.Left = i
.Top = j
.Width = l
.Height = k
If bFlip Then .Flip (msoFlipHorizontal)
With .Line
.Style = msoLineSingle
.Weight = 0.75
End With
End With
End Sub

However if I drawn the line sloping bottom left to top right starting at the
botttom (for all intent and purpose the same line) then that property
returns false and the same code results in a flipped line :-(



Tony said:
Hi Greg,

Take a look at the HorizontalFlip property of the Shape - it should
be True for a line sloping top right to bottom left.

--
Greg Maxey

See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.

"It is not the critic who counts, not the man who points out how the
strong man stumbles, or where the doer of deeds could have done them
better. The credit belongs to the man in the arena, whose face is
marred by dust and sweat and blood, who strives valiantly...who knows
the great enthusiasms, the great devotions, who spends himself in a
worthy cause, who at the best knows in the end the triumph of high
achievement, and who at the worst, if he fails, at least fails while
daring greatly, so that his place shall never be with those cold and
timid souls who have never known neither victory nor defeat." - TR
 
G

Greg Maxey

George,

Roger on the improperly declared variables. I knew better and was just
being sloppy and careless.


George said:
Did you ever get an answer?

The problem is that the line is defined by its bounding box, so there
is no lineStart or lineEnd per se. Instead:

Sub Test()
Dim oShp As Word.Shape
Dim oShpNew As Word.Shape
Dim i, j, k, l As Long

Debug.Print "i is of type: " & VarType(i)
Debug.Print "l is of type: " & VarType(l)

Dim theMargin As Long 'Page margin of the page. Needed to offset
the line properly.
theMargin = 36

Set oShp = Selection.ShapeRange(1)
i = oShp.Left + theMargin
j = oShp.Top + theMargin
k = oShp.Height
l = oShp.Width
oShp.Delete

Set oShpNew = ActiveDocument.Shapes.AddLine(i, k + j, l + i, j)
With oShpNew.Line
.Style = msoLineThinThin
.Weight = 3
End With
End Sub

There is also a minor mistake with the dim statement. Multiple values
are not assigned that way; only the last one is a true long. The two
debug statements show that they are different types.

--
Greg Maxey

See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.

"It is not the critic who counts, not the man who points out how the
strong man stumbles, or where the doer of deeds could have done them
better. The credit belongs to the man in the arena, whose face is
marred by dust and sweat and blood, who strives valiantly...who knows
the great enthusiasms, the great devotions, who spends himself in a
worthy cause, who at the best knows in the end the triumph of high
achievement, and who at the worst, if he fails, at least fails while
daring greatly, so that his place shall never be with those cold and
timid souls who have never known neither victory nor defeat." - TR
 
T

Tony Jollans

Hi Greg,

The Flip properties, taken together, should tell you all you need to know.

If HorizontalFlip is set, the line starts at the right
If VerticalFlip is set, the line starts at the bottom

So if you copy the vertical flip and the horizontal flip you should get what
you want.
 
G

Greg Maxey

Tony,

Yes. That will work. I can't just copy and then apply as both those are
read only properties, but I can 1 .find out and 2. if not the same change.
Final code which seems to be working in all cases is:

Sub DuplicateAndReplaceLine()
Dim oShp As Word.Shape
Dim bHFlip As Boolean
Dim bVFlip As Boolean
Dim oShpNew As Word.Shape
Dim i As Long, j As Long, k As Long, l As Long
Dim lngBAL As Long, lngBAS As Long, lngBAW As Long
Dim lngEAL As Long, lngEAS As Long, lngEAW As Long
Dim lngStyle As Long
Dim pWeight As String
Set oShp = Selection.ShapeRange(1)
i = oShp.Left
j = oShp.Top
k = oShp.Height
l = oShp.Width
bHFlip = oShp.HorizontalFlip
bVFlip = oShp.VerticalFlip
With oShp.Line
lngStyle = .Style
pWeight = .Weight
lngBAL = .BeginArrowheadLength
lngBAS = .BeginArrowheadStyle
lngBAW = .BeginArrowheadWidth
lngEAL = .EndArrowheadLength
lngEAS = .EndArrowheadStyle
lngEAW = .EndArrowheadWidth
End With
oShp.Delete
Set oShpNew = ActiveDocument.Shapes.AddLine(i, j, i + 72, j)
With oShpNew
.Left = i
.Top = j
.Width = l
.Height = k
If .HorizontalFlip <> bHFlip Then .Flip (msoFlipHorizontal)
If .VerticalFlip <> bVFlip Then .Flip (msoFlipVertical)
With .Line
.Style = lngStyle
.Weight = pWeight
.BeginArrowheadLength = lngBAL
.BeginArrowheadStyle = lngBAS
.BeginArrowheadWidth = lngBAW
.EndArrowheadLength = lngEAL
.EndArrowheadStyle = lngEAS
.EndArrowheadWidth = lngEAW
End With
End With
End Sub

Thanks.


--
Greg Maxey

See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.

"It is not the critic who counts, not the man who points out how the strong
man stumbles, or where the doer of deeds could have done them better. The
credit belongs to the man in the arena, whose face is marred by dust and
sweat and blood, who strives valiantly...who knows the great enthusiasms,
the great devotions, who spends himself in a worthy cause, who at the best
knows in the end the triumph of high achievement, and who at the worst, if
he fails, at least fails while daring greatly, so that his place shall never
be with those cold and timid souls who have never known neither victory nor
defeat." - TR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top