Automating AutoCAD using Microsoft Access VBA

Automating AutoCAD using Microsoft Access VBA

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

Automating AutoCAD using Microsoft Access VBA

Anonymous
Not applicable

I have a little knowledge in VBA programming and requires some help in using MS Access VBA to use AutoCAD to measure the dimensions of a drawing and retrieve this information from AutoCAD and display it to the user. Can anyone tell me if there's any possible way to do this? Thank you so much

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

Norman_Yuan
Mentor
Mentor

Directly to your question: yes, it is more than possible to do it. You do it via AutoCAD COM automation. That is, you run your application (in your case, it is VBA code in Access), which automates AutoCAD (either starts a new AutoCAD instance, or find a running AutoCAD instance), makes AutoCAD open drawings, update/retrieve data to/from drawings...

 

However, before you do this, you really need to ask yourself/users: do you want to run 2 apps (Access UI and AutoCAD) in order to achieve the business goal? Since AutoCAD has to run anyway, can the business goal be reached withing AutoCAD (i.e. only writing AutoCAD VBA code)?

 

Anyway, if you have decided to use another application to control AutoCAD (be it Access, or Excel, or anything else for that mater), following VBA code would let you connected to AutoCAD and do something with it (hopefully you do know AutoCAD well and know very basic AutoCAD COM object model; otherwise, you need to do quite some study before you can do anything meaningful with AutoCAD):

 

Sub DoSomethingWithAcad()

  Dim cadApp As AcadApplication

  Dim dwg As AcadDocument

  On Error Resume Next

  Set cadApp = GetObject(, "AutoCAD.Application")

  If Err.Number<>0 Then

     Set cadApp = CreateApplication("AutoCAD.Application")

  End If

 If cadApp Is Nothing Then

    MsgBox "Cannot connect to/start AutoCAD." & vbCrlf & _

                   "Something is wrong with the computer!"

    Exit Sub

  End If

 

  On Error GoTo 0

  Set dwg = cadApp.Open ("[drawing file path/name]")

  DoSomethingWithDrawing dwg

  dwg.Close True/False '' Close the drawing with or without saving changes

End Sub

 

Private Sub DoSomethingWithDrawing(dwg As AcadDocument)

   ''You access drawing data here

   '' You can also update drawing here

End Sub

 

Of course, in your app (Access/Excel/...) the VBA project must set reference to AutoCAD type library.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

Anonymous
Not applicable

thank you so much for your reply. If I would like to automatically rename my drawing layers to something, how should I go about it?

0 Likes
Message 4 of 5

Norman_Yuan
Mentor
Mentor

Since you are trying to change something in AutoCAD from external application (Access UI), it does make thing a bit complicated if your AutoCAD programming knowledge is limited. Again, I do not recommend such a solution in most cases (automating AutoCAD from external app), unless the working to be done in AutoCAD is trivial/simple and has to be done by user who knows little of using AutoCAD (yet, AutoCAD must be installed and running!).

 

Anyway, if you go ahead with what you wanted (using Access to control AutoCAD), then take the code in my previous reply:

...

Private Sub DoSomethingWithDrawing(dwg As AcadDocument)

   ''' Here you rename a particular layer

   RenameLayer dwg,  oldName, newName

 

   '' Do whatever else

  ....

End Sub

 

Private Sub RenameLayer(dwg As AcadDocument, oldName As String, newName As String)

    Dim lay  As AcadLayer

    For Each lay In dwg.Layers

        If UCase(lay.Name) = UCase(oldName) Then

            lay.Name = newName

            Exit For

        End If

    Next

End Sub

 

Agian and again, when you are new to AutoCAD programming, try to do and learn it with AutoCAD VBA where you can easily write and test/debug your code, such as the Sub I just wrote here (RenameLayer()). AutoCAD VBA also provides quite good help document with tons of sample code: simply select any class/property/method in AutoCAD VBA's Object Browser window and click "?", which would open the help document/sample code.

 

Once you are comfortable to write AutoCAD VBA Code to do something, you can then move the code to run from Access application side with some, but not too much, necessary modification, if you insists to do thing from external app that controls AutoCAD.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 5

Anonymous
Not applicable

Hi, Member,

 

I want a VBA code(In excel) for automate import and export attributes of autocad with dynamic changes.

If anyone have the same ?

 

Nielsh

0 Likes