Consulting

Results 1 to 10 of 10

Thread: open acad drawings within excel VBA

  1. #1

    open acad drawings within excel VBA

    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
    Last edited by Aussiebear; 04-09-2023 at 04:39 AM. Reason: Adjusted the code tags

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi gary lee,

    Welcome to VBAX

    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.
    Last edited by Aussiebear; 04-09-2023 at 04:40 AM. Reason: Adjusted the code tags

  3. #3
    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
    Last edited by Aussiebear; 04-09-2023 at 04:40 AM. Reason: Adjusted the code tags

  4. #4
    The following code works to check if autocad is already running

    [vba]
    Dim AcadApp as AcadApplication

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

    great thanks to Tommy

  5. #5
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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
    Last edited by Aussiebear; 04-09-2023 at 04:41 AM. Reason: Adjusted the code tags

  6. #6
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Quote Originally Posted by garylee
    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.
    Last edited by Aussiebear; 04-09-2023 at 04:42 AM. Reason: Adjusted the code tags

  7. #7
    great thanks to Tommy,,

    I learned another keyword.

    "On Error" is great for handling error message

  8. #8
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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

  9. #9
    VBAX Regular
    Joined
    Sep 2005
    Posts
    8
    Location

    Excel 2010

    Quote Originally Posted by garylee
    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
    Last edited by Aussiebear; 04-09-2023 at 04:43 AM. Reason: Adjusted the code tags

  10. #10
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •