- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've been attempting to read Excel files for data extraction of project data and just when I thought I had it all figured out along comes a new headache in the testing phase. I've been testing the routines on my local drive and they've worked splendidly, but when I attempt to open a file from a network location then the silent features I thought were in place no longer work as Windows is now asking me if I wish to save the file before closing. It's the exact same routine I'm using for my local, I just can't figure out why when it's a network location I suddenly need to verify a close without saving. The goal is to open and read the data without any interaction from the user. Below are my Open and Close subroutines, anyone know what I'm missing?
I'd also like to know how to open the file as read-only if you know the coding for that as well; could score you some bonus points.
;--- Routine to OPEN an existing Excel file
;--- Call routine with path & filename (Example: OpenExcel "C:/Test.xlsx")
;--- File will be opened hidden
(defun OpenExcel (Exfile)
(setq MyFile (findfile Exfile)) ;double check file exists at location
(if Myfile ;nil = file not found
(progn ;if file found open it
(setq MyXL (vlax-get-or-create-object "Excel.Application")) ;find Excel application
(vla-put-visible MyXL :vlax-false) ;hide application from view
(vlax-put-property MyXL 'DisplayAlerts :vlax-false) ;hide Excel alerts
(setq MyBook (vl-catch-all-apply 'vla-open (list (vlax-get-property MyXL "WorkBooks") MyFile)))
))
) ;return - MyFile = nil if file not found
;--- Routine to CLOSE Excel file & Session
;--- Routine to CLOSE Excel file & Session
;--- Assumes previously opened with OpenExcel function
(defun CloseExcel (/)
(if Myfile (progn ;if filename still stored then...
(vl-catch-all-apply 'vlax-invoke-method (list MyBook "Close")) ;close the file
(vl-catch-all-apply 'vlax-invoke-method (list MyXL "Quit")) ;release the app
(setq MyFile nil MyXL nil MyBook nil MySheet nil MyRange nil MyTab nil MyCell nil ExCell nil)
(gc) ;garbage cleanup
))) ;return

Solved! Go to Solution.