Link to home
Start Free TrialLog in
Avatar of twowheel
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.
Avatar of DrTribos
DrTribos
Flag of Australia image

activedocument.Shapes.AddLine 50, 50, 100, 100
Avatar of twowheel
twowheel

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.
that is the line of code that you would put in your macro...  the numbers are the start and end points of the line as plotted on an x-y chart.

You should add to this and explore a bit using intellisense...
User generated image
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

Open in new window

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
Oh...

I am not sure how to do that ...
ASKER CERTIFIED SOLUTION
Avatar of DrTribos
DrTribos
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial