Consulting

Results 1 to 4 of 4

Thread: Run a Routine When Opening AutoCAD Drawing

  1. #1
    VBAX Regular
    Joined
    Mar 2005
    Posts
    11
    Location

    Run a Routine When Opening AutoCAD Drawing

    Bear with me, I'm a beginner with VBA. What I want to do is run a VBA routine automatically each time an existing drawing is openned.

    How???

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

    Welcome to VBAexpress!

    In acad create a new project, follow the instructions below. Save the project. Then pick Tools -> Load Application, in the dialog box that appears look for Startup Suite, Pick the Contents Button, another dialog box will appear browse to the location you saved the project to and pick the project. Pick close and close. The next time you open acad and a drawing/create a new drawing it will run your code.

    in a module named Module1
    [VBA]
    Sub RunThis()
    'place code you want to run here
    End Sub
    [/VBA]
    in a class called Class1
    [VBA]
    Public WithEvents ACADApp As AcadApplication
    Public Sub AcadApplication_Events()
    Set ACADApp = GetObject(, "AutoCAD.Application")
    End Sub
    Private Sub ACADApp_EndOpen(ByVal FileName As String)
    RunMacro "Module1.RunThis"
    End Sub
    Private Sub ACADApp_NewDrawing()
    RunMacro "Module1.RunThis"
    End Sub

    [/VBA]

    in the ThisDocument code pane place this
    [VBA]
    Dim Aok As Class1
    Private Sub AcadDocument_Activate()
    If Aok Is Nothing Then Set Aok = New Class1
    If IsEmpty(ACADApp) Then Aok.AcadApplication_Events
    End Sub

    [/VBA]

    Happy Coding

  3. #3
    VBAX Regular
    Joined
    Mar 2005
    Posts
    11
    Location
    Thanks, works great!

  4. #4
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Good Deal! Let me know if I can help some more

Posting Permissions

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