PDA

View Full Version : [SOLVED] open acad drawings within excel VBA



garylee
06-06-2005, 06:43 AM
I would like to know how to open an autocad drawing within excel VBA.

I wrote the following code but it gives me a run-time error '419'
ActiveX component can't create object


Dim AcadApp as Acadapplication
Dim MyDwg as AcadDocument
Dim DWG as string ' file path

Set AcadApp = GetObject(, "AutoCAD.Application")
Set MyDwg = AcadApp.Documents.Open(DWG)

reply feel free to contact me if more info required..


thank you very much

gary lee
email: spedia3214@yahoo.com

Tommy
06-06-2005, 06:58 AM
Hi gary lee, :hi:

Welcome to VBAX :yes

When posting code use the vba tags it will display the code as if it was in the vbaide :)



Dim AcadApp as Acadapplication
Dim MyDwg as AcadDocument
Dim DWG as string ' file path
Set AcadApp = GetObject(, "AutoCAD.Application")
Set MyDwg = AcadApp.Documents.Open(DWG) '<- the DWG should contain "C:\acad\somedrawing.dwg"


The way I use it



Public Graphics As AcadApplication
Public Sub OpnAcad()
Dim AngBracDwg As String
Set Graphics = GetObject(, "AutoCAD.Application")
If Err.Description > vbNullString Then
Err.Clear
Set Graphics = CreateObject("AutoCAD.Application")
End If
AngBracDwg = "c:\dwg\ba1.dwg"
Graphics.Documents.Open (AngBracDwg)
End Sub



Let me know if more assistance is needed.

garylee
06-07-2005, 08:46 AM
Tommy:

the error message still there..

Error occurs right after the "GetObject" line is exercuted
The program end there

The "Err.Description" line did not function.

I tried the following, still doesn't work..


If IsError(GetObject(, "AutoCAD.Application")) Then
Err.Clear
Set AcadApp = CreateObject("AutoCAD.Application")
Else
Set AcadApp = GetObject(, "AutoCAD.Application")
End If




Any idea how to check if autocad is already running??
autocad running --> use GetObject
autocad not running --> use CreateObject

thank you

garylee
06-07-2005, 09:00 AM
The following code works to check if autocad is already running


Dim AcadApp as AcadApplication

If AcadApp Is Nothing Then
Set AcadApp = CreateObject("AutoCAD.Application")
Else
Set AcadApp = GetObject(, "AutoCAD.Application")
End If


great thanks to Tommy

Tommy
06-07-2005, 09:05 AM
If you don't have a reference checked in the tools > reference > AutoCad xxxx type library it will have errors, the getobject function will not work. I am sorry I forgot to tell you that.


Public Graphics As AcadApplication
Public Sub OpnAcad()
Dim AngBracDwg As String
On Error Resume Next 'I added this line
Set Graphics = GetObject(, "AutoCAD.Application")
If Err.Description > vbNullString Then
Err.Clear
Set Graphics = CreateObject("AutoCAD.Application")
End If
AngBracDwg = "c:\dwg\ba1.dwg"
Graphics.Documents.Open (AngBracDwg)
On Error goto 0
End Sub

Tommy
06-07-2005, 10:23 AM
The following code works to check if autocad is already running



Dim AcadApp as AcadApplication
If AcadApp Is Nothing Then
Set AcadApp = CreateObject("AutoCAD.Application")
Else
Set AcadApp = GetObject(, "AutoCAD.Application")
End If


great thanks to Tommy
garylee,

All the code you have posted does is check to see if the variable has been inialized. But just to test open acad and step through it, what you will see is the code will execute for "Set AcadApp = CreateObject("AutoCAD.Application")" will execute no matter if acad is already open or not.

garylee
06-07-2005, 12:08 PM
great thanks to Tommy,,

I learned another keyword.

"On Error" is great for handling error message

Tommy
06-07-2005, 12:55 PM
LOL Yes it is.

On Error Resume Next - this means if an error occurs resume cause you are looking for it put err.clear in some statement afterwards to clear the error

On Error goto "ErrLabel" - means goto "ErrLabel" There should be Err.clear and "clean up variables" then exit (don't want any errors floating around to mess things up later)

Then at the end of the sub/function place on error goto 0 - this turns off error checking. I have found that if you don't it may come back and mislead you as to where the actual error occured. Real PAIN to debug the app :)

Don't hesitate to ask more, we have a lot of real good coders around here, sometimes it takes a few posts to get to the problem, but we hang till it's done. :)

See you in the next Question :)

kato01
01-01-2012, 06:41 PM
The following code works to check if autocad is already running


Dim AcadApp as AcadApplication
If AcadApp Is Nothing Then
Set AcadApp = CreateObject("AutoCAD.Application")
Else
Set AcadApp = GetObject(, "AutoCAD.Application")
End If


great thanks to Tommy


I just tried this on VBA of EXCEL 2010. It simply does not work. I get the following error message:
user-defined type not defined
when I debug, the error refers to the line below


Dim AcadApp As AcadApplication

Am I missing something?

Thanks

Tommy
01-03-2012, 11:20 AM
Most likely it is because you do not have a referance to the acad library.
First you must have acad installed on your system or the system that you are developing on. Then you need to select the AutoCad xxxx type library. This is when it should work. :)