twowheel
asked on
Writting a VBA to draw line
I would like to know how to write a macro in VBA for msword 2007 such that by activating the macro (through assigning a hotkey), I can activate the same draw line function as in Insert -> Shape -> Line. This is because at times, I need to draw a shape with many lines and using Insert -> Shape -> Line is too time-consuming.
Currently, my drawline macro is
Sub drawline()
SendKeys ("%nsh"), True
SendKeys ("{DOWN}{DOWN}{ENTER}"), True
End Sub
The above macro activate the keypress Insert -> Shape -> Line.
Is there a more straightforward way.
The line inserted must have a starting point at the cursor position, same effect as
Insert -> Shape -> Line.
Thank you.
Currently, my drawline macro is
Sub drawline()
SendKeys ("%nsh"), True
SendKeys ("{DOWN}{DOWN}{ENTER}"), True
End Sub
The above macro activate the keypress Insert -> Shape -> Line.
Is there a more straightforward way.
The line inserted must have a starting point at the cursor position, same effect as
Insert -> Shape -> Line.
Thank you.
activedocument.Shapes.AddL ine 50, 50, 100, 100
ASKER
But I do not want my line to have a fixed starting point, I want it to be at the cursor position and the end point can be dragged before the line is drawn.
well you can change the start point so they equate to the selection co-ordinates...
Sub DrawLine()
Dim oLine As Word.Shape
Dim x As Single
Dim y As Single
x = Selection.Information(wdHorizontalPositionRelativeToPage)
y = Selection.Information(wdVerticalPositionRelativeToPage)
Set oLine = ActiveDocument.Shapes.AddLine(x, y, x + 100, y + 100)
With oLine
' start a new line of code in here with a .
' the . becomes short hand for 'oLine.'
' in here - I mean in this 'with' block
End With
oLine.Select
End Sub
ASKER
Sorry, I think I have made a mistake in my original question.
What I meant is that
The line inserted must have a starting point as the mouse position (not cursor position),
so that when I left click, I can drag to determine the end point of the line.
Same effect as Insert -> Shape -> Line.
Please refer to the attached docx.
Steps-to-be-taken.docx
What I meant is that
The line inserted must have a starting point as the mouse position (not cursor position),
so that when I left click, I can drag to determine the end point of the line.
Same effect as Insert -> Shape -> Line.
Please refer to the attached docx.
Steps-to-be-taken.docx
Oh...
I am not sure how to do that ...
I am not sure how to do that ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.