create or open excel file

create or open excel file

Anonymous
Not applicable
3,391 Views
4 Replies
Message 1 of 5

create or open excel file

Anonymous
Not applicable

I am trying to create an excel file using autocad vba using following code but I get compile error as User-defined type not defined

 

Dim oApp_Excel As Excel.Application
Dim oBook As Excel.workbook

Set oApp_Excel = CreateObject("EXCEL.APPLICATION")
Set oBook = oApp_Excel.workbooks.Add

0 Likes
Accepted solutions (1)
3,392 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor
Accepted solution

You need to add "Microsoft Excel [Version] Library" as reference (VBAIDE->Menu->Tools->References...)

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5

Anonymous
Not applicable

thanks norman

0 Likes
Message 4 of 5

Anonymous
Not applicable

I want to output into excel the name, layer and center co-ordinate of selected circle . I succeed in name and layer but could not get centre co-ordinate.

I used following code.

.

wrks.Range("A" & rownr) = obj.Name
wrks.Range("B" & rownr) = obj.Layer
wrks.Range("C" & rownr) = obj.EffectiveName
wrks.Range("D" & rownr) = obj.CentrePointx
wrks.Range("E" & rownr) = obj.CentrePointy
wrks.Range("F" & rownr) = obj.CentrePointz

0 Likes
Message 5 of 5

norman.yuan
Mentor
Mentor

It looks like you need a bit study on what type of objects you are dealing with in AutoCAD. You can open AutoCAD, go into VBA Editor, open Object Browser window and see all the objects available in AutoCAD object model.

 

From the few lines of code, it is not clear what the "obj" is: a block reference (AcadBlockReference), or a circle (AcadCircle)?

 

If it is the former, then an AcadBlockReferece does nto have property like "CenterPoint..."; if it is an AcadCircle, it should have a property Center, but not CentrePointX/Y/Z and it does not have property "Name" or "EffectiveName"

 

So, you need to know WHAT AcadEntity or AcadEntities you are dealing with, and know what properties the target entity has.

 

Now, assume you are targeting a circle (AcadCircle), and needs to know its centre's coordinate, you do:

 

Range(...)=obj.Center[0]

Range(...)=obj.Center[1]

Range(...)=obj.Center[2]

 

Because Center propery is an 3-element array of Double values, representing X,Y and Z of coordinate

 

If you are targeting block, then there is no property Center, Instead, it has InsertionPoint property.

 

If you are targeting mixed entity types, you then need to treat them differently with the help of 

 

If TypeOf obj is AcadCicle Then

  ''Do something

ElseIf TyoeOf obj Is AadBlockReference Then

  ....

Else

  ....

End If

 

Norman Yuan

Drive CAD With Code

EESignature