Author Topic: open specific sheet in excel  (Read 2659 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
open specific sheet in excel
« on: February 23, 2009, 03:12:11 PM »
I have this code that will allow me to write to a specific cell in excel
however im fumbling about trying to figure out how to open an excel file to the specific sheet to write to
can anyone post a sample syntax on how to open an excel file to a specific sheet?


here is the code that i have
thanks

Code: [Select]
(vl-load-com)
;to make a link between acad and Excel (ok with Excel 97)
(setq ObjExcel (vlax-get-or-create-object "Excel.Application"))
(setq ObjWorkbooks (vlax-get-property ObjExcel "workbooks"))
(setq ObjCurrWorkbooks (vla-add ObjWorkbooks))
(setq ObjSheet (vlax-get-property ObjExcel "activesheet"))
(setq ObjCells (vlax-get-property ObjSheet "cells"))


;to put a value in a cell
(setq lineXls 4)
(setq rowXls 6)

(setq valueXls  "test")
(vlax-put-property ObjCells 'item lineXls rowXls valueXls)

ronjonp

  • Needs a day job
  • Posts: 7529
Re: open specific sheet in excel
« Reply #1 on: February 23, 2009, 03:28:49 PM »
Try something like this:

Code: [Select]
(vlax-for sht (vlax-get objcurrworkbooks 'sheets)
  (if (= (vlax-get sht 'name) "namehere")
    (setq objsheet sht)
  )
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: open specific sheet in excel
« Reply #2 on: February 23, 2009, 03:35:02 PM »
i need a little more help if you will sir.
i placed it at the bottom of the lisp, didnt seem to do anything.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: open specific sheet in excel
« Reply #3 on: February 23, 2009, 04:07:24 PM »
This works for me:

Code: [Select]
(vl-load-com)
 ;to make a link between acad and Excel (ok with Excel 97)
(and (setq objexcel (vlax-get-or-create-object "Excel.Application"))
     (setq objworkbooks (vlax-get-property objexcel "workbooks"))
     (setq objcurrworkbooks (vla-add objworkbooks))
     (vlax-for sht (vlax-get objcurrworkbooks 'sheets)
       (if (= (vlax-get sht 'name) "Sheet3")
         (progn (setq objsheet sht) (vlax-invoke objsheet 'activate))
       )
     )
     (setq objcells (vlax-get objsheet "cells"))
     (vlax-put-property objcells 'item 3 3 "test")
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

chauny274

  • Guest
Re: open specific sheet in excel
« Reply #4 on: February 24, 2009, 01:47:30 PM »
http://web2.airmail.net/terrycad/AutoLISP-Code.htm

I reccomend going there and getting "getexcel.lsp" for free. I've used for a program and it works great, just read the instructions and be sure to use the closeexcel command to close the sheet when your done.

These programs help create, read, and write to excel sheets.