Forms

P

PraxisPete

Hi Everybody, I am using Excel 2002 and I have only been using vb for a short
while

I need to draw arcs and I have Pick-up this code on the net which does not
run. The error is Run-time error ‘424’: Object required. I think its some
thing to do with Form1 but I do not understand about Forms and I cannot find
anything about forms in my ref books apart from Userforms.

Can somebody please explain?

Many Thanks

Private Declare Function Arc Lib "gdi32" (ByVal hdc As Long, ByVal X1 As
Long, _
ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long,
ByVal Y3 As Long, _
ByVal X4 As Long, ByVal Y4 As Long) As Long


Private Sub Form_Load()
Dim rval As Long
'play about with the values
rval = Arc(Form1.hdc, 100, 450, 300, 10, 250, 100, 100, 150)

End Sub
 
T

Tom Ogilvy

That code is for VB the language, where the work is done on a form.

I don't see any argument on it to specify a radius, so I would go back to
the code offered previously and see if you can't figure out how to translate
the parameters you have into the parameters you need.
 
P

PraxisPete

Thank you for your comments Tom,

I have found this code which meets my requirements i.e. has an argument for
Radius and I can provide the position for the centre, start angle and sweep
angle, this would be perfect. Is there anyway this can run, or is there a
similar tool or addin ?

Thanks in anticipation

Declare Function AngleArc Lib "gdi32.dll" (ByVal hdc As Long, ByVal x As
Long,
ByVal y As Long, ByVal dwRadius As Long, ByVal eStartAngle As Single, ByVal
eSweepAngle As Single) As Long


' Draw an arc formed by the upper half of a circle (from 0 to 180
' degrees counterclockwise). The circle is centered at (100, 150) and has a
radius
' of 50. The arc is drawn using the solid black stock pen.
Dim hpen As Long ' handle to the black stock pen
Dim holdpen As Long ' handle to Form1's previously selected pen
Dim retval As Long ' return value

' Get the solid black stock pen and select it for use in Form1.
hpen = GetStockObject(BLACK_PEN) ' get the pen's handle
holdpen = SelectObject(Form1.hDC, hpen) ' select the pen

' Make sure arcs are drawn going counterclockwise
retval = SetArcDirection(Form1.hDC, AD_COUNTERCLOCKWISE)
' Draw the arc
retval = AngleArc(Form1.hDC, 100, 150, 50, 0, 180)

' Select Form1's previous pen to restore the "defaults".
retval = SelectObject(Form1.hDC, holdpen) ' select the old pen
 
A

Alok

This code also will only work with traditional VB rather than VBA. This is
because in VBA the UserForms do not have the hdc property for you to get the
Handle to the form. Have you considered using the arc object in Excel instead.

Alok Joshi
 
T

Tom Ogilvy

This will draw an arc with the center at the upper left corner of D5 and a
radius of 50. It draws a 90 degree arc (I am speaking from a reference of
the top of the arc being at top of the sheet). I then adjust it to be 135
degrees. The degrees for adjustment appear to be measured from horizontal
with counter clockwise being positive. So 90 from the top is 0 degress for
adjustment. However, that establishes the end of the Arc. The actual Arc
is extended in a clockwise direction. So if I had done 135 rather than -
45, I get an arc of 315 degress measured from the top (or - 45 from top).

You can then turn on the macro recorder and use the rotate command if you
don't want the arc to start from something other than the top. Also look at
the flip command (vertical and horizontal) to get a different orientation.

Sub Macro4()
Dim sh As Shape
Dim cTop As Long
Dim cLeft As Long
Dim rad As Long
cTop = Range("D5").Top
cLeft = Range("D5").Left
rad = Range("D5").Width
Set sh = ActiveSheet.Shapes.AddShape(msoShapeArc, cLeft, cTop - rad, rad,
rad)
sh.Adjustments(2) = -45
'sh.Adjustments(2) = 135
End Sub
 
P

PraxisPete

Thank you Tom and Alok I have got it cracked now and drawing just what I wanted
Its surprising to me just how much can be done in Excel if you now how.
Once again a big Thank you
 

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