excel to autocad with vba

excel to autocad with vba

Anonymous
Not applicable
1,052 Views
4 Replies
Message 1 of 5

excel to autocad with vba

Anonymous
Not applicable

hello,

i tried to write a little code that takes text from excel table's cell and write it in autocad but still can't execute it.

********************************************************

Sub DrawAutocadline()

Dim AutocadApplication As Autocad.Application
Dim startline(0 To 2) As Double
Dim endline(0 To 2) As Double
Set AutocadApplication = CreatObject("Autocad.Application")
startline(0) = Cells(2, 2).Value
startline(1) = Cells(3, 2).Value
endline(0) = Cells(2, 3).Value
endline(1) = Cells(3, 3).Value
AutocadApplication.ActiveDocument.ModelSpace.AddLine(startline, endline)

End Sub

****************************************************

if someone can correct my code or suggest me another one i will be grateful.

Thanks

0 Likes
1,053 Views
4 Replies
Replies (4)
Message 2 of 5

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> but still can't execute it

What happens when you try it?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 5

Anonymous
Not applicable

firts it shows me that my code is incorrect and i dont know how to handle this cuz am still new at this:

error compilation, expect =

thanks

0 Likes
Message 4 of 5

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> it shows me that my code is incorrect

Which code? Which line?

 

Please understand that we are not sitting behind you seeing what message you get!

Things like "not working" or "incorrect" without getting all the information you get on your display is not enough for us to reply with valid suggestions.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 5

Anonymous
Not applicable

hello,

yes i am sorry for not beeing clear 🙂

but i have resolved this problem thanks to a code that i think link autocad with excel, it works.

*****************************************************************************************************

Sub Connect_Acad()

On Error Resume Next

Set acadApp = Interaction.GetObject(, "AutoCAD.Application")
'Set acadApp = Interaction.GetObject(, "AutoCAD.Application.22")
'both statements above behave without any discernible difference

If Err Then
Debug.Print "ERROR " & Err.Number
Debug.Print Err.Description
Debug.Print "starting autocad"

Err.Clear

Set acadApp = New AcadApplication
'Set acadApp = Interaction.CreateObject("AutoCAD.Application.22")
'both statements above behave without any discernible difference

'essential statement
acadApp.Visible = True

If Err Then
MsgBox Err.Description
Exit Sub
End If
End If

Debug.Print "Now running " + acadApp.Name + " version " + acadApp.Version

Set acadDoc = acadApp.ActiveDocument
If acadDoc Is Nothing Then
Set acadDoc = acadApp.Documents.Add
End If

If acadDoc.ActiveSpace = 0 Then
acadDoc.ActiveSpace = 1
End If

End Sub

Sub DrawAutocadline()

Call Connect_Acad

Dim lineObj As AcadLine
Dim startline(0 To 2) As Double
Dim endline(0 To 2) As Double
startline(0) = Cells(2, 2).Value
startline(1) = Cells(3, 2).Value
endline(0) = Cells(2, 3).Value
endline(1) = Cells(3, 3).Value
Set lineObj = acadDoc.ModelSpace.AddLine(startline, endline)
ZoomAll
End Sub

*************************************************************************************************

this is the code that works for me, thank you very much sir, i really appreciate your interest in helping me.