add code at runtime

Discussion in 'AutoCAD' started by lorier, Jul 14, 2004.

  1. lorier

    lorier Guest

    Hi
    Does anyone have any examples of how to programmatically add code at runtime? Past NG have mentioned that with Tools>References> and checking "Microsoft Visual Basic for Applications Extensibilty 5.3" this is possible with the VBE object model.
    Thanks
     
    lorier, Jul 14, 2004
    #1
  2. I haven't used the VBE Object Model but I do have this link http://www.cpearson.com/excel/vbe.htm that has some good examples. I don't think it includes one to do what you want but it my get you headed in the right direction.
    Regards - Nathan
     
    Nathan Taylor, Jul 14, 2004
    #2
  3. lorier

    lorier Guest

    Thanks,
    The examples are good for excel and word, but i'm still unsure how you reference the acadproject, or if its even possible now.
     
    lorier, Jul 15, 2004
    #3
  4. Here is an example that was posted on the AUGI Exchange by Ed Jobe.

    Code:
    
    Note: The VBE object is defined in "Microsoft Visual Basic for Applications Extensibilty 5.3".
    For this to work, you must first provide a reference to it in the VBAIDE through
    Tools>References and selecting/checking the above file. For more information, see
    Exchange tip  EX001199 - vba tips.txt.
    
    Public Sub EnumCode()
    'Ed Jobe © 2002
    'enumerate the procedures in a project
    Dim objVB As VBE
    Dim objComponents As VBComponents
    Dim objComponent As VBComponent
    Dim objCM As CodeModule
    Dim cnt As Integer
    Dim l As Long
    Dim strLine As String
    Dim strProc As String
    Dim strProc1 As String
    
    Set objVB = ThisDrawing.Application.VBE
    Set objComponents = objVB.ActiveVBProject.VBComponents
    For Each objComponent In objComponents
    Set objCM = objComponent.CodeModule
    Debug.Print objComponent.Name
    cnt = objCM.CountOfLines
    For l = 1 To cnt
    strLine = objCM.Lines(l, 1)
    strProc = objCM.ProcOfLine(l, vbext_pk_Proc)
    'only print procedure line once per procedure
    If strProc <> "" And strProc <> strProc1 Then
    Debug.Print vbTab & strProc
    strProc1 = strProc
    End If
    Next l
    Next objComponent
    End Sub
    
    
    Regards - Nathan
     
    Nathan Taylor, Jul 15, 2004
    #4
  5. lorier

    lorier Guest

    so i guess something like this wouild add and run a new sub, cool...

    Public Sub addAndRunSub()
    Dim objVB As VBE
    Dim objComponents As VBComponents
    Dim objComponent As VBComponent
    Dim objCM As CodeModule
    Set objVB = THISDRAWING.Application.VBE
    Set objComponents = objVB.ActiveVBProject.VBComponents
    vbext_pk_Proc)Set objComponent = objComponents.Add(vbext_ct_StdModule)
    objComponent.Name = "testmodule"
    objComponent.CodeModule.AddFromString ("sub test_2" & vbCr & "msgbox(" & Chr(34) & "hi" & Chr(34) & ")" & vbCr & "end sub")
    test_1
    End Sub
    Sub test_1()
    test_2
    End Sub

    thanks!
     
    lorier, Jul 16, 2004
    #5
  6. lorier

    lorier Guest

    omitting the "vbext_pk_Proc)"
     
    lorier, Jul 16, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.