Linking to Excel for data retrieval

Discussion in 'AutoCAD' started by Rodney Mishmash, Apr 23, 2004.

  1. I currently have a LISP routine that retrieves information to draw a part
    from a text file(1 item per row). The text file is currenty produced by a C
    or C++ program we had written by an outside vendor. In order to make changes
    to the geometry easier to make, I am producing an Excel spreadsheet to do
    all of the calculations. Is there a way to link the drawing to Excel and
    read the 'variables' with the lisp. I have tried linking through dbconnect,
    but am at a loss when I have to read the values once linked. Any help or
    direction would be great.
     
    Rodney Mishmash, Apr 23, 2004
    #1
  2. This was in my reply to - ChrisW - "Visual Lisp & Excel" 4/8/4 at 2:20pm

    This is a program used to write to the current active cell in an excel
    spread sheet.
    Part of the code reads the current cell to make sure it is blank (stored to
    XLtxt).
    You can modify this code to read the cell(s) in the current Active Sheet of
    an Excel Spread Sheet.

    (defun ACAD2XL (XLTEXT /)
    (vl-load-com) ;load extended AutoLISP functions (must be run prior to VL
    functions)
    ;get excel application
    (setq XLapp (vlax-get-or-create-object "Excel.Application")
    XLrun (vlax-get-property XLapp 'Visible)
    )
    ;check if excel running
    (if (= XLrun :vlax-false)
    ;excel is not running
    (progn
    (vlax-release-object XLapp)
    (setq XLapp nil XLrun nil)
    (gc)
    (alert "\nERROR - Please Start Excel and select Cell (to place data)
    prior to running this command.")
    )
    ;excel is running
    (progn
    (setq XLwbk (vlax-get-property XLapp 'WorkBooks)
    XLash (vlax-get-property XLapp 'ActiveSheet)
    XLcel (vlax-get-property XLapp 'ActiveCell)
    )
    ;check if workbook is active
    (if (and XLash XLcel)
    ;excel workbook is active
    (progn
    (setq XLcol (vlax-get-property XLcel 'Column)
    XLrow (vlax-get-property XLcel 'Row)
    XLcva (vlax-get-property XLcel 'Text)
    XLtxt (vlax-variant-value XLcva)
    )
    ;convert column and row number into Cell text (Example - Column 1,
    Row 1 = A1
    (if (= (/ XLcol 26) 0)
    (setq TC (strcat (chr (+ (rem XLcol 26) 64)) (itoa XLrow)))
    (setq TC (strcat (chr (+ (/ XLcol 26) 64)) (chr (+ (rem XLcol
    26) 64)) (itoa XLrow)))
    )
    (setq CELL (strcat TC ":" TC))
    ;check if cell all ready has data
    (if (= XLtxt "")
    (setq YN "Y")
    (setq YN (strcase (getstring (strcat "\nNOTICE - Excel Cell
    contains [" XLtxt "] - Change [n]=No or [Y]=Yes ? "))))
    )
    (if (member YN (list "" "Y"))
    (progn
    (setq Cells (vlax-get-property XLash 'Range CELL))
    (setq VTXT (vlax-make-variant XLTEXT vlax-vbString))
    (vlax-put-property Cells 'Value2 VTXT)
    )
    )
    ;release objects
    (vlax-release-object Cells)
    (vlax-release-object XLcel)
    (vlax-release-object XLash)
    (vlax-release-object XLwbk)
    (vlax-release-object XLapp)
    (setq XLapp nil XLrun nil XLwbk nil XLash nil XLcel nil Cells nil)
    (gc)
    )
    ;excel is running, but no active workbooks
    (progn
    (alert "\nERROR - Excel is running, but there are NO workbooks
    open.")
    (vlax-release-object XLwbk)
    (vlax-release-object XLapp)
    (setq XLapp nil XLrun nil XLwbk nil)
    )
    )
    )
    )
    (princ)
    )
     
    Alan Henderson, Apr 23, 2004
    #2
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.