Sending Keystrokes to Acad LT from Excell VBA

Discussion in 'AutoCAD' started by Joe, Mar 14, 2007.

  1. Joe

    Joe Guest

    I know that VBA has the ability to send keystrokes to another program,
    even if that program isn't programmed to work with VBA. I want to do
    this so that I can do some simple programming on AutoCAD LT 2000.

    I know I could write the AutoCAD commands to a text file, and then
    manually run it as a script from inside Acad LT, but I want to
    automate that process.

    Another desire (although it is a low priority one) is to read some of
    the results of List command. Eg. I send AutoCAD the commands to list
    a particular

    I can assume that the Acad LT is already open, and the dwg I want to
    use is also already open.

    The problem I have, how to tell Excel's VBA which program window to
    use with the SendKeys statement. Can someone give me a specific
    example of how to do this?

    Joe Dunfee
     
    Joe, Mar 14, 2007
    #1
  2. Joe

    Joe Guest

    I"ve made a little bit of progress, but continue to have problems.

    I use the following code to activate the AutoCAD window from inside an
    Excel VBA routine, and then send keystrokes

    AppActivate "AutoCAD", 0
    SendKeys "Line 0,0 10,5", false

    But the space between the two coordinates are ignored for some reason
    and they are merged to say "0,010,5"

    So, I tried separating the elements by conctantenating, but that
    failed as well. Finally, I used a separate SendKeys command to send
    an enter. This works;
    =========================
    AppActivate "AutoCAD", 0

    SendKeys "Line 0,0", False
    SendKeys "{enter}", False
    SendKeys "10,20", False
    SendKeys "{enter}", False
    SendKeys "{enter}", True
    =========================

    That was a bit more cumbersome, but it did work.

    Now, I am having problems with other similar routines. For some
    reason some of the SendKeys commands seem to be skipped over. The
    keystrokes are never sent. No error message or anything.

    Any ideas what is happening?

    Joe Dunfee
     
    Joe, Mar 14, 2007
    #2
  3. Joe

    CADmechanic Guest

    Hi Joe,

    Try implementing "DoEvents" to help with the missing keystrokes.

    I think you would have better luck with posting to the customization
    newsgroup.

    If you get really stuck, you can email me or contact me through
    cadmechanic.com as I don't frequent newsgroups on a regular basis.

    Hope that helps.
     
    CADmechanic, Mar 14, 2007
    #3
  4. Joe

    Joe Guest

    Thanks for the reply. Are the DoEvents are just to kill time between
    keystrokes? I started to do some for-next loops, but apparently
    wasn't doing enough. I finally went went for 5,000 loops, inside
    5,000 loops (25 million loops) and was able to slow it down enought to
    watch each command... and it worked!

    I've been able to get output from AutoCAD by turning on the log file.
    [logifleon] and also making it so that it doesn't pause every screen
    when I do a lengthy listing [using qaflags to 2 - this is a non
    supported variable that works in version 2000 or earlier. I don't
    know its current equivalent]

    Joe Dunfee
     
    Joe, Mar 15, 2007
    #4
  5. Joe

    jan.Baeyens Guest

    Why don't you write everything to a file in your VBA program and then
    ask autocad to process the file with sending keystrokes.
    This takes away lot of the monitoring problems and lets you test your
    program lots easier.
    I do this trick with dos prompts and unix shell commands.

    Jan
     
    jan.Baeyens, Mar 16, 2007
    #5
  6. Joe

    Joe Guest

    That is what I wanted to do... but the only way I had to get
    everything over to VBA is to list the entities to a log file. I can't
    do a lot of processing in AutoCAD LT because it has very limited
    abilities of this sort compared to the full AutoCAD. Or did I
    misunderstand what you were suggesting?

    Joe Dunfee
     
    Joe, Mar 17, 2007
    #6
  7. Joe

    jan.Baeyens Guest

    Joe
    First of all I'd like to mention that autocad has a activex interface
    you can use from VBA. I don't know wheter autocad LT has it. You
    should check in VBA by clicking tools reference and look for stuff
    that starts with autocad.

    If you don't find that; sendkeys is the way to go.
    emulates keystrokes on the keyboard. Programs who do (part) of the
    keyboard processing themselves and don't use the standard windows
    messages (wm key down wm key up, wm char...) will not respond as
    expected. Also programs who swap focus on keystrokes will not behave
    as you would expect. As soon as windows are created you need to insert
    delays which are platform dependent. So I prefer to avoid it, if at
    all possible. But if it is needed and the program accepts files in and
    out you still can have a structured approach. Note that autocad used
    to be a dos program and is not 100% windows GUI compliant so I expect
    it to have "wierd behaviour"

    FI if you want to do your line command "Line 0,0 10,5" I would have
    my VBA program create a file "doit.cmd"
    then use sendkeys to "run the file" doit.cmd and if I'm interested in
    the outcome I would use logging to parse the file afterwards.

    I havn't done this with autocad but I have done this successfully with
    lots of other programs.
    Below I copied some code I use for a dos box called rundoscommand.
    the code rundoscommand "dir > log.txt" gives me a file that contans
    the output of the dir command. And I know the file is available when
    the function returns.

    You could create something simular for autocad. RunAutocadCommand.
    simularly you could have something like
    runautocadcommand "a command to turn logging on" & vbnewline & "Line
    0,0 10,5" & vbnewline & "a command to stop logging"
    And then some code to parse the log file
    The biggest problem you'll have is to know autocad stopped processing
    your file. What you should do is investigate the GUI and try to find
    out wether there is a easy way to know. FI a windows title changing.
    And if nothing does you can have your last comand do something it you
    can notice. Something like saveas with a new filename.

    To conclude: I would investigate the activex path. If that doesn't
    work be prepared for blood sweat and tears. But use files and limit
    the direct autocad communication.
    Jan


    'This function executes a dos command.
    'To run several commands at the same time use the vbnewline vatiable
    to seperate the commands.
    'It works as follows.
    'A bat file is created containing the command.
    'The bat file is executed.
    'We wait for the bat file to exit.
    Public Function RunDosCommand(stCommand As String) As Long
    Dim stTmpFile As String
    stTmpFile = "runcommand.bat"
    Open stTmpFile For Output As #2
    Print #2, stCommand
    Close #2
    Dim shell As IWshShell_Class
    Set shell = New IWshShell_Class
    RunDosCommand = shell.Run(stTmpFile, , True)
    End Function
     
    jan.Baeyens, Mar 18, 2007
    #7
  8. Joe

    Joe Guest

    Mr Baeyens, thank you for the detailed response.

    I did need to go the SendKeys approach, beause I am currently using
    AutoCAD LT, and that is really my option. I simply had to slow VBA
    down a lot. I did however, reduce the effort inside of VBA by writing
    a few short routines using the available macro ability inside of
    AutoCAD LT. The process is not something I can really give to another
    user, because it still requires several manuals steps. (i.e. run
    script from inside AcadLT, then switch to Excel and run VBA Macro,
    then choose Log file....etc.)

    I have IntelliCAD with VBA, but its VBA is different from AutoCAD and
    very poorly documented. But, they are supposed to release an AutoCAD
    comptable VBA module later this year. So perhaps I will eventually do
    it all from inside IntelliCAD.

    Joe Dunfee
     
    Joe, Mar 30, 2007
    #8
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.