Is there a way to startapp Excel without knowing the program path?

Is there a way to startapp Excel without knowing the program path?

jamieq
Collaborator Collaborator
1,356 Views
5 Replies
Message 1 of 6

Is there a way to startapp Excel without knowing the program path?

jamieq
Collaborator
Collaborator

I want to open a CSV file in Excel and have Excel be visible and active. I tried:

(setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
(vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open filename)
(vla-put-visible *ExcelApp% :vlax-true)
(vlax-release-object *ExcelApp%)(gc)

But this wouldn't make Excel visible if another excel file was already open. The only other way I know to open an external program is with startapp. However, that only works if you know the path to Excel. I want this to work on many computers, with unknown versions of Excel, thus unknown paths. Is there a way to find the local path to Excel from AutoCAD?

0 Likes
Accepted solutions (2)
1,357 Views
5 Replies
Replies (5)
Message 2 of 6

dlanorh
Advisor
Advisor
Accepted solution

@jamieq wrote:

I want to open a CSV file in Excel and have Excel be visible and active. I tried:

(setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
(vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open filename)
(vla-put-visible *ExcelApp% :vlax-true)
(vlax-release-object *ExcelApp%)(gc)

But this wouldn't make Excel visible if another excel file was already open. The only other way I know to open an external program is with startapp. However, that only works if you know the path to Excel. I want this to work on many computers, with unknown versions of Excel, thus unknown paths. Is there a way to find the local path to Excel from AutoCAD?


Look here http://www.lee-mac.com/findfile.html

 

(LM:findfile "Excel.exe" "C:/")

Works on my computer, although it took 10-15 seconds. You could then add the folder to the Autocad search path.

I am not one of the robots you're looking for

0 Likes
Message 3 of 6

jamieq
Collaborator
Collaborator

I should've known Lee would have the solution I was looking for. Thank you! I searched his site for Excel, but didn't even think to look for a findfile alternative. My own lisp programming would be nowhere without him. 

0 Likes
Message 4 of 6

dlanorh
Advisor
Advisor
Accepted solution

Going back to your original problem, this might be an interesting read.

 

https://www.theswamp.org/index.php?topic=50575.msg557259#msg557259

I am not one of the robots you're looking for

Message 5 of 6

jamieq
Collaborator
Collaborator

Lee Mac FTW again!! I have no idea how you found that. I searched like CRAZY for Excel related threads. Guess I just didn't have the right search terms. 

Anyway, I did get that method working perfectly, so now I don't have to worry about different paths to search. It took a bit more digging to understand just how the 'appactivate method works, but I got it. This is how my code looks now:

(setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))
(vlax-invoke-method (vlax-get-property *ExcelApp% 'WorkBooks) 'Open filename)
(vlax-invoke-method (vlax-get-or-create-object "Wscript.Shell") 'appactivate "Excel")
(vlax-release-object *ExcelApp%)(gc)

Once again, thanks very much, dlanorh!

0 Likes
Message 6 of 6

dlanorh
Advisor
Advisor
I had a nagging feeling I had seen the problem before. I tried invoking the appactivate method on the excel object using an index (0,1 etc) until it failed; but only succeeded in activating everything else (calculator, word etc) , which is when I searched "appactivate excel" at The Swamp.

I am not one of the robots you're looking for

0 Likes