AutoCAD Electrical General

Reply
Contributor
Nicolas.L
Posts: 13
Registered: ‎12-05-2012
Message 1 of 3 (2,585 Views)
Accepted Solution

[VBA] Writing a data from excel to autocad without openning autocad manuelly

2585 Views, 2 Replies
12-11-2012 05:38 AM

Hello,

 

I have trouble in my task. I am trying to write the data of an excel cell into autocad ( string data ).

The following code is almost right but it doesn't write the text in autocad.

(open autocad and add drawing only)

 

If anyone has an idea on the problem.

 

Regards

 

Nicolas

 

Code VBA:

 

Public ThisDrawing As AcadObject

 

Sub test()

Dim xAP As Excel.Application
Dim xWB As Excel.Workbook
Dim xWS As Excel.Worksheet
Dim Height As Double
Dim P(0 To 2) As Double
Dim TxtObj As AcadText
Dim TxtStr As String
Dim AcadObj As AcadApplication
Dim AcadDoc As AcadDocument

Set xAP = Excel.Application
Set xWB = xAP.Workbooks.Open("C:\Path\Name.xls")
Set xWS = xWB.Worksheets("Sheet1")
MsgBox "Excel says: """ & Cells(1, 1) & """"

 
On Error Resume Next
Set AcadObj = GetObject(, "AutoCAD.Application")
If Err.Number <> 0 Then
    Set AcadObj = CreateObject("AutoCAD.Application")
    NombreDessinAutocad = 2
End If
AcadObj.Visible = True
 
    MsgBox "Step_1"
       
Set AcadDoc = AcadObj.Application.Documents.Add
MsgBox AcadObj.name & " version " & AcadObj.Version & _
" is running."
Set AcadUtil = AcadDoc.Utility
Set AcadEspaceObj = AcadDoc.ModelSpace
  
    MsgBox "Step_2"

 

Height = 10
P(0) = 1: P(1) = 1: P(2) = 0
TxtStr = Cells(1, 1)
   
    MsgBox TxtStr
   
Set TxtObj = AcadObj.ModelSpace.AddText(TxtStr, P, Height)    <------doesn't work
Set TxtObj = ThisDrawing.ModelSpace.AddText(TxtStr, P, Height)      <------doesn't work


    MsgBox "End"
 

End Sub

Contributor
Nicolas.L
Posts: 13
Registered: ‎12-05-2012
Message 2 of 3 (2,560 Views)

Re: [VBA] Writing a data from excel to autocad without openning autocad manuelly

12-13-2012 06:00 AM in reply to: Nicolas.L

Hi,

 

This was a little mystake, her is the solution:

 

Public ThisDrawing As AcadObject

 

Sub test()

Dim xAP As Excel.Application
Dim xWB As Excel.Workbook
Dim xWS As Excel.Worksheet
Dim Height As Double
Dim P(0 To 2) As Double
Dim TxtObj As AcadText
Dim TxtStr As String
Dim AcadObj As AcadApplication
Dim AcadDoc As AcadDocument

Set xAP = Excel.Application
Set xWB = xAP.Workbooks.Open("C:\Path\Name.xls")
Set xWS = xWB.Worksheets("Sheet1")
MsgBox "Excel says: """ & Cells(1, 1) & """"

 
On Error Resume Next
Set AcadObj = GetObject(, "AutoCAD.Application")
If Err.Number <> 0 Then
    Set AcadObj = CreateObject("AutoCAD.Application")
    NombreDessinAutocad = 2
End If
AcadObj.Visible = True
 
    MsgBox "Step_1"
       
Set AcadDoc = AcadObj.Application.Documents.Add
MsgBox AcadObj.name & " version " & AcadObj.Version & _
" is running."
Set AcadUtil = AcadDoc.Utility
Set AcadEspaceObj = AcadDoc.ModelSpace
  
    MsgBox "Step_2"

 

Height = 10
P(0) = 1: P(1) = 1: P(2) = 0
TxtStr = Cells(1, 1)
   
    MsgBox TxtStr
   
'Set TxtObj = AcadObj.ModelSpace.AddText(TxtStr, P, Height)    <------doesn't work
'Set TxtObj = ThisDrawing.ModelSpace.AddText(TxtStr, P, Height)      <------doesn't work

Set TxtObj = AcadEspaceObj.AddText(TxtStr, P, Height) <-------- Work !


    MsgBox "End"
 

End Sub

New Member
noveldecor
Posts: 1
Registered: ‎06-22-2013
Message 3 of 3 (1,987 Views)

Re: [VBA] Writing a data from excel to autocad without openning autocad manuelly

06-22-2013 04:14 AM in reply to: Nicolas.L

Hi

 

Thanks for this post.

I am wanting to do a similar thing.

 

Can you attached a sample Excel data file along with the Autolisp code which i can load in my autocad file.

 

Best Wishes

-Arian

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Manufacturing Community
The Manufacturing Community provides additional access to tips, tutorials, blogs and networking with peers.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

New AutoCAD Electrical Category!

The AutoCAD Electrical forum has moved into it's very own category page, and can no longer be found within the Additional Product Forums.

Ask the Community