AddRegion Fails - Run-Time error 91 - Object variable or with block variable not set SEE CODE BELOW

AddRegion Fails - Run-Time error 91 - Object variable or with block variable not set SEE CODE BELOW

xxaaron
Participant Participant
1,511 Views
5 Replies
Message 1 of 6

AddRegion Fails - Run-Time error 91 - Object variable or with block variable not set SEE CODE BELOW

xxaaron
Participant
Participant

pi = 3.xxx-xxxxxxxx
Dim StartPoint(0 To 2) As Double
Dim EndPoint(0 To 2) As Double
Dim Line(0 To 3) As Object

StartPoint(0) = 0: StartPoint(1) = 0: StartPoint(2) = 0
EndPoint(0) = 1: EndPoint(1) = 0: EndPoint(2) = 0
Set Line(0) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)
StartPoint(0) = 1: StartPoint(1) = 0: StartPoint(2) = 0
EndPoint(0) = 1: EndPoint(1) = 1: EndPoint(2) = 0
Set Line(1) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)
StartPoint(0) = 1: StartPoint(1) = 1: StartPoint(2) = 0
EndPoint(0) = 0: EndPoint(1) = 1: EndPoint(2) = 0
Set Line(2) = ThisDrawing.ModelSpace.AddLine(StartPoint, EndPoint)

StartPoint(0) = 0: StartPoint(1) = 0.5: StartPoint(2) = 0

Set Line(3) = ThisDrawing.ModelSpace.AddArc(StartPoint, 0.5, pi / 2, 3 * pi / 2)


Dim Region1 As AcadRegion
Region1 = ThisDrawing.ModelSpace.AddRegion(Line)

0 Likes
Accepted solutions (2)
1,512 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor
Accepted solution

In the following 2 lines of your code, there are 2 errors:

 

Dim Region1 As AcadRegion
Region1 = ThisDrawing.ModelSpace.AddRegion(Line)

 

1. since Region1 is declared as AcadRegion object, when assigning an object value to it, you must use "Set" keyword, like:

 

Set Region1 = ....

 

This is why the error message saying "... object variable...no set..."

 

2. However, the real error of your code is that your code treats the return value of AddRegion() method wrong: the returned value is AN ARRAY of AcadRegion object (i.e. one or more region objects), not a single AcadRegion object. 

 

That is, the code should be

 

Dim regions As Variant
regions = ThisDrawing.ModelSpace.AddRegion(Line)

 

Then, you can look up the returned array for all possible regions. If you are sure the lines would only form one region, you can

 

Dim region As AcadRegion

Set region = regions(0)

'' Then do somethoing with the region

... ...

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

xxaaron
Participant
Participant
Accepted solution
Hi Norman,

Thank you very much, it worked perfectly.

Aaron Cowen, PE
0 Likes
Message 4 of 6

abhi.rathod
Community Visitor
Community Visitor

Actually I have created a shape using polyline and trying to make region out of it so that using massprop i can get area, inertia, centroid. But i am getting runtime error 424. Please help me resolve this issue for following code:

 

Private Sub CommandButton1_Click()

Dim acadapp As AcadApplication 'creating autocad application
Dim dwg As AcadDocument
Dim polyL As AcadLWPolyline
Dim copoly() As Double
Dim i As Integer
Dim j As Integer
Dim k As Integer


Dim lastrow As Integer
''''''''''''''''''''''''''''''''
'check if autocad is open
On Error Resume Next
Set acadapp = GetObject(, "AutoCAD.Application")
On Error GoTo 0

'IF autocad app is not opened create a new instance and make it visible.
If acadapp Is Nothing Then
Set acadapp = New AcadApplication
acadapp.Visible = True
End If

'Check if there is an active drawing running
On Error Resume Next
Set dwg = acadapp.ActiveDocument
On Error GoTo 0

'No active drawing found. Create a new one.
If dwg Is Nothing Then
Set dwg = acadapp.Documents.Add
acadapp.Visible = True
End If
'''''''''''''''''''''''''
Sheet1.Activate

 

lastrow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
ReDim copoly((2 * lastrow) - 1)

k = 0
For i = 1 To lastrow
For j = 1 To 2
copoly(k) = Sheet1.Cells(i, j)
k = k + 1
Next j
Next i


Set polyL = dwg.ModelSpace.AddLightWeightPolyline(copoly)
polyL.Closed = True

Dim region As Variant
region = thisdrawing.ModelSpace.AddRegion(polyL)

End Sub

0 Likes
Message 5 of 6

Ed__Jobe
Mentor
Mentor

I think you should have started your own thread on this problem.

 

Have you tried setting a breakpoint and stepping through the code to find the line at which you get an error?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 6 of 6

abhi.rathod
Community Visitor
Community Visitor

yes  definitely.  Code is running perfectly and creating a random shape using coordinates through polyline but in last line it is not able form a region and showing runtime error.

0 Likes