How to release object after using activeX within Lisp

Discussion in 'AutoCAD' started by Calvin, Sep 17, 2003.

  1. Calvin

    Calvin Guest

    Hi,

    I have a lisp which transfer the Attribute data direct to MS Excel.
    Everything seem ok except the MX-Sort routine. (The code in red)
    After execute the lisp the object did not release. (Can find out in Windows
    Task Manager)
    Herein, partial of the lisp.
    Please advise. Many thanks...



    (defun c:pds_xl (/ xlrange ceobj sheetobj wb-obj xlobj xlv)
    (vl-load-com)
    (setq xlv (pds_get_excel_lib))
    (pds_create_excel_sheet)
    (setq xlrange (cons 5 15)) ;range set just for testing
    (pds_sort_xl xlrange) ;if skip, the object will be released without
    problem.
    (pds-excel-quit)
    ) ;pds_xl


    (defun pds_get_excel_lib (/ sys:drv office:dir office:dir1 office:dir2
    exlib)
    (if (= (setq sys:drv (getenv "systemdrive")) nil)
    (setq sys:drv "c:")
    )
    (setq office:dir (strcat sys:drv "\\program files\\microsoft office\\"))
    (setq office:dir1 "office\\"
    office:dir2 "office10\\"
    )
    (cond ((setq exlib (findfile (strcat office:dir office:dir1
    "excel8.olb")))
    (setq xlv "ex97")
    ) ;excel 97 & 98
    ((setq exlib (findfile (strcat office:dir office:dir1 "excel9.olb")))
    (setq xlv "ex2k")
    ) ;excel 2000
    ((setq exlib (findfile (strcat office:dir office:dir1 "excel.exe")))
    (setq xlv "exxp")
    ) ;excel xp
    ((setq exlib (findfile (strcat office:dir office:dir2 "excel.exe")))
    (setq xlv "exxp")
    )
    (t (setq exlib nil))
    ) ;cond
    (if (null mx-acos)
    (if exlib
    (vlax-import-type-library
    :tlb-filename exlib
    :methods-prefix "mx-"
    :properties-prefix "mx-"
    :constants-prefix "mxc-"
    )
    (alert "excel typelib not exist")
    ) ;if
    ) ; if mx-acos
    xlv
    ) ;pds_get_excel_lib

    (defun pds_create_excel_sheet ()
    (setq xlobj (vlax-create-object "excel.application"))
    (vla-put-visible xlobj 1)
    (setq wb-obj (vlax-invoke-method
    (vlax-get-property xlobj 'workbooks)
    'add
    )
    )
    (setq sheetobj (mx-get-activesheet wb-obj))
    ) ;pds_create_excel_sheet


    (defun pds_sort_xl (xlrange)
    (mx-sort
    (mx-get-range
    sheetobj
    (strcat "b1:"
    (chr (+ (cdr xlrange) 65))
    (itoa (car xlrange))
    )
    )
    (mx-get-range sheetobj "b1")
    mxc-xlascending
    nil
    nil
    nil
    nil
    nil
    mxc-xlyes
    nil
    nil
    mxc-xltoptobottom
    nil
    ) ;problem part!
    (mx-select
    (mx-get-range sheetobj (strcat "b" (itoa (car xlrange))))
    )
    )


    (defun pds-excel-quit ()
    (vlax-invoke-method
    wb-obj "saveas" "c:\\ex-test1.xls" nil nil nil nil nil nil
    )
    (vla-put-visible xlobj 0)
    (vlax-release-object sheetobj)
    (mx-close wb-obj)
    (vlax-release-object wb-obj)
    (vlax-invoke-method xlobj 'quit)
    (vlax-release-object xlobj)
    (mapcar '(lambda (x) (set x nil)) '(wb-obj sheetobj xlobj))
    (gc)
    ) ;pds-excel-quit
     
    Calvin, Sep 17, 2003
    #1
  2. Calvin

    Mark Guest

    very carefully release the objects in their correct order,
    then set all the corresponding variable to nil, then use gc twice.
    just at first glance i do not see any range object being released.
     
    Mark, Sep 17, 2003
    #2
  3. See:

    Visual LISP ActiveX API for Excel - reposted

    Customer Files 5/30/2003




    --
    ________________________________________________

    Marc'Antonio Alessi (TV) Italy
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    O.S. = XP Pro 2002 - Sp.1 - Ita
    AutoCAD = 2002 Ita - Sp.1
    _VERNUM = "K.0.44"
    ACADVER = "15.06s (LMS Tech)"
    (ver) = "Visual LISP 2000 (it)"
    ________________________________________________
     
    Marc'Antonio Alessi, Sep 17, 2003
    #3
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.