Can someone help me simplify this. It shouldn't take this much code
(defun c:excel()
(vl-load-com)
(setq xl (vlax-get-or-create-object "Excel.Application"))
(setq value7 (vlax-get-property xl "RANGE" "E7"))
(setq value8 (vlax-get-property xl "RANGE" "F7"))
(setq value9 (vlax-get-property xl "RANGE" "G7"))
(setq VE7 (vlax-variant-value (vlax-get-property value7 "text")))
(setq VE8 (vlax-variant-value (vlax-get-property value8 "text")))
(setq VE9 (vlax-variant-value (vlax-get-property value9 "text")))
(setq newlist (list VE7 VE8 VE9))
)
Thanks!
@gcsjlewis wrote:Can someone help me simplify this. It shouldn't take this much code
(defun c:excel()
(vl-load-com)
(setq xl (vlax-get-or-create-object "Excel.Application"))
(setq value7 (vlax-get-property xl "RANGE" "E7"))
(setq value8 (vlax-get-property xl "RANGE" "F7"))
(setq value9 (vlax-get-property xl "RANGE" "G7"))
(setq VE7 (vlax-variant-value (vlax-get-property value7 "text")))
(setq VE8 (vlax-variant-value (vlax-get-property value8 "text")))
(setq VE9 (vlax-variant-value (vlax-get-property value9 "text")))
(setq newlist (list VE7 VE8 VE9))
)....
I haven't worked with Excel from within AutoCAD, but just in terms of structure, you could eliminate a bunch of variables that are used only once, and therefore may as well just be "calculated" right where they're used. And you can set more than one variable inside one (setq) function, so you don't need a separate one for each. Try:
(defun c:excel (/ xl)
(vl-load-com)
(setq
xl (vlax-get-or-create-object "Excel.Application")
newlist
(list
(vlax-variant-value (vlax-get-property (vlax-get-property xl "RANGE" "E7") "text"))
(vlax-variant-value (vlax-get-property (vlax-get-property xl "RANGE" "F7") "text"))
(vlax-variant-value (vlax-get-property (vlax-get-property xl "RANGE" "G7") "text"))
); end list and newlist variable
); end setq
); end defun
Or, consolidating some of the remaining duplication into a subroutine:
(defun c:excel (/ val xl)
(vl-load-com)
(defun val (ltr)
(vlax-variant-value (vlax-get-property (vlax-get-property xl "RANGE" (strcat ltr "7")) "text"))
); end defun -- val
(setq
xl (vlax-get-or-create-object "Excel.Application")
newlist (list (val "E") (val "F") (val "G"))
); end setq
); end defun -- c:excel