autoload vba

Discussion in 'AutoCAD' started by perry, Feb 2, 2004.

  1. perry

    perry Guest

    I want to load some VBA routines each time a drawing is opened (without
    using an "acad.dvb" file).
    I know I can do this in an "acaddoc.lsp" file with a few lines that look
    something like this:

    (if (findfile "perry.dvb")
    (command "_-vbaload" "perry")
    (alert "personal VBA not loaded.")
    )

    The problem with this approach is that upon opening subsequent drawings I
    get an alert box stating that the vba module is already loaded.
    With autolisp files I can use a line like this:
    (defun C:LM () (if (not c:lman)(load "lman"))(c:lman))
    which will fist check if the routine is already loaded, then load and
    execute if it is not. So my question is can I do something similiar with
    VBA?
    Something along the line of:
    (if (not VBAPROJECTNAME)(vbaload "VBAPROJECTNAME"))(macroname))

    Thanks
     
    perry, Feb 2, 2004
    #1
  2. perry

    developer Guest

    Use (command "VBARUN" ...) instead of "VBALOAD" in your ACADDOC.LSP
     
    developer, Feb 2, 2004
    #2
  3. Hi Perry,

    As you would expect there is a collection of loaded programs which can be
    investigated, but there is no point in going to all that trouble.

    The VBARUN command and it's variants will load (if necessary) and run the
    program. The best way to run your VBA macros, is to define lisp functions
    to run them. Put the lisp programs in your custom menu file, say PERRY.MNL
    .. These functions get loaded when you load the PERRY.MNU, or PERRY.MNS, or
    PERRY.MNC file as the case may be.

    A typical example is:

    (setq sLandToolboxPath
    (strcat
    (vl-registry-read
    "HKEY_LOCAL_MACHINE\\Software\\CADApps\\LandToolbox" "VBA Program Path")
    "\\"
    )
    )
    In this case the program is installed by "Inno Setup" which writes the
    installation path to the registry. The code above reads the registry and
    gets the path. You could easily simply hard code a path here.

    The code below adds the file and macro name to the path and then uses
    vl-vbarun to run the function. vl-vbarun runs the macro, loading it if
    necessary.

    (defun C:CA_DuplicatePointsAbout ()
    (setq sLandToolboxFunction (strcat sLandToolboxPath
    "CADApps_DuplicatePointsL3.dvb!DuplicatePointsAbout"))
    (vl-vbarun sLandToolboxFunction)
    (CADApps_BlankCommandLine sLandToolboxFunction)
    )

    The code below tidies up the command line when you run the macro

    (defun CADApps_BlankCommandLine (spString)
    (setq n (strlen spString))
    (repeat n(prompt(chr 8)))
    (repeat n(prompt(chr 32)))
    (princ)
    )

    The MNU (or MNS) file contains
    CA_DupPts [ About Duplicate Points]^C^C^CCA_DuplicatePointsAbout;

    Advantages of this process are that you can:
    press <Enter> to repeat the menu command.
    type the "CA_DuplicatePointsAbout " command at the keyboard.
    the macro is only loaded if used.
    the lisp is only loaded if you load the menu.
    by having the path in the registry it's easy to maintain the system if you
    wish to make changes to the program location with an installation program..


    --


    Laurie Comerford
    CADApps
    www.cadapps.com.au
     
    Laurie Comerford, Feb 2, 2004
    #3
  4. perry

    perry Guest

    Well, that doesnt quite work, it will look for a macro to run, not a project
    to load. If I give it a macro name it will still fail because the
    project the macro resides in isnt yet loaded.
     
    perry, Feb 2, 2004
    #4
  5. perry

    developer Guest

    Sorry, it worked for me because my project is named ACAD.DVB
     
    developer, Feb 2, 2004
    #5
  6. perry

    perry Guest

    Yeah, I was trying to get away from using the "acad.dvb" (easy to
    loose/overwrite), but I may have to
     
    perry, Feb 2, 2004
    #6
  7. perry

    developer Guest

    Hey Perry, check out AutoCAD's online help for the VBARUN command. I think you can still use that command to achieve what you want.
     
    developer, Feb 2, 2004
    #7
  8. perry

    developer Guest

    My ACADDOC.LSP has the following line in S::STARTUP

    (command "VBARUN" "L:\\CAD\\VBA\\ACAD.DVB!DwgMod.DwgStartup")

    You can use the full path to your project, the bang (exclamation point), the module name, and the macro name. I assure you the project will load if it is not loaded.
     
    developer, Feb 2, 2004
    #8
  9. perry

    perry Guest

    Yes, I did see that and it is another alternative, I was avoiding it for
    reasons similar to the acad.dvb issues. Its easy to overwrite or mess up
    the acaddoc/s::startup routines.
    I guess I'll just settle on using an acad.dvb file.
    Thanks for the tips D ;)

    --
    Perry Leets
    Inovec Optimization and Control Systems
    Eugene, Oregon
    the module name, and the macro name. I assure you the project will load if
    it is not loaded.
     
    perry, Feb 2, 2004
    #9
  10. perry

    perry Guest

    Well, Im not real happy with any of these methods. My first method worked
    with the one exception I mentioned.
    Using an acad.dvb file, I dont even know if it actually loads as the code
    doesnt execute.
    To clarify, this is a small vb project which simply "hooks" the endsave
    event and creates a .dwf file of the current .dwg.
    If an "acad.dvb" file exists, must you do something else to initialize the
    VB environment?
    What would be really helpful would be a way to determine if a particular vba
    project or macro is loaded, then I could test for this
    in my inital code snippent and not issue the "vbaload" if its already
    loaded.
    This is all so ridiculously easy with lisp...
     
    perry, Feb 2, 2004
    #10
  11. perry

    Mark Propst Guest

    I imagine you already know this, but just in case, do you have an acad.rx
    file in support path?
    with one of the following lines
    acadvba.arx ;(if 2002)
    ;acvba.arx ;(if 2004)

    that initializes vba before any macros run
     
    Mark Propst, Feb 2, 2004
    #11
  12. perry

    perry Guest

    Yeah, I was just about to make a post on that Mark.
    My "acad.dvb" file wasnt working cuz I didnt have an "acad.rx" file, so I
    created said file containing
    just one line "acadvba.arx", now the code in my acad.dvb DOES load and
    execute properly (hooking the save event).
    I guess if I wanted to distribute this my setup program would have to check
    for the existance of both an acad.dvb and
    acad.rx file and create/modify them accordingly.
    Thanks
     
    perry, Feb 2, 2004
    #12
  13. perry

    Mark Propst Guest

    Glad ya got it!
    :)~
    plus you'd have to check acadver if you were writing an .rx in a setup
    routine to determine which line to put in it! and if they have both
    installed on same machine you'd have to get support path for each one and
    write two .rx's, one for each path, (unless there's a way to write an if
    statement in the rx to read acadver...I have no idea how .rx is actually
    read and if you can include a conditional loop in it)
     
    Mark Propst, Feb 3, 2004
    #13
  14. Search the lisp discussion group for a thread "DVB file already loaded" dated Jan/30/02 by Menzi, Jürg

    He has a lisp function to return a list of all loaded dvb projects so you can use

    (if (not (member "MyVbaProjectName" (VxGetLoadedVbaPojs)))
    (vl-vbaload "MyVbaProjectName.dvb")
    )
     
    Mark Gardiner, Feb 5, 2004
    #14
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.