Jump to content

Excel file. Open, run macro, save and close.


Kowal

Recommended Posts

(defun c:runm (/ abks aexc asht find x xbks xlpath xshts)
  (if (not (findfile
	     (setq xlpath (strcat (getvar "dwgprefix") "4" ".xlsm"))));<-- full path of your Excel file, change to suit
    (progn
      (alert "File does not exist")
      (exit)
      (princ)
      );progn
    );if
  (alert "Close Excel File Save Changes")

  (setq aexc (vlax-get-or-create-object "Excel.Application"))
  (setq xbks (vlax-get-property aexc "Workbooks"))
  (if (not (vl-file-systime xlpath))
    (progn
      (setq find nil)
      (setq abks (vlax-invoke-method xbks "Add"))
      );progn
    (progn
      (setq find T)
      (setq abks (vlax-invoke-method xbks "Open" xlpath))
      );progn
    );if
  (setq xshts (vlax-get-property abks "Sheets"))
  (setq asht (vlax-get-property xshts "Item" 1)) ;<-- sheet number - optional not used in this program
  (vla-put-visible aexc :vlax-true)
  (vlax-put-property aexc "ScreenUpdating" :vlax-true) ; to see changes
  (vlax-put-property aexc "DisplayAlerts" :vlax-true) ; to initialize user interaction
  (vlax-invoke-method asht "Activate") ;optional not used in this program
  (vlax-invoke-method aexc "Run" "Print_all") ;<-- macro name in the Excel file
  (vlax-invoke xbks "Close")
(vlax-invoke aexc "Quit")
  (princ)
  )

Hi,

I would like to do this scenario: open the .xlsm file, run the macro in the .xlsm file, save the changes, close the file.

The only thing I miss is saving the changes before closing.

 

If I remove the code responsible for running macros, the files are closed.

 (vlax-invoke-method aexc "Run" "Print_all")

 

Please help me save the changes after running the macro and closing the file.

 

If someone has a similar working lisp please share it.

 

Link to comment
Share on other sites

I have no idea what is in your macro but have you thought about putting that code into Autocad so it does the same thing as the macro ? 

 

The other way is have your excel macro talk to Autocad its the same method open a Application. 

 

Attached is an example macro of Excel making objects in Autocad.

 

(defun closeexcel ( / )
(if (= ah:ex "T")
(vlax-invoke-method (vlax-get-property myxl "ActiveWorkbook") 'Close :vlax-False)
(vlax-invoke-method (vlax-get-property myxl "ActiveWorkbook")  "SaveAs" filename -4143 "" "" :vlax-false :vlax-false nil )
)

 

 

excel vba draw acad.txt

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...