vlax-get-object creates new Excel instance even when a workbook is open

vlax-get-object creates new Excel instance even when a workbook is open

deheylen
Contributor Contributor
1,787 Views
7 Replies
Message 1 of 8

vlax-get-object creates new Excel instance even when a workbook is open

deheylen
Contributor
Contributor

I am trying to retrieve the current selection from an open excel workbook. When I run this code:

(setq ^ExcelApp (vlax-get-object "Excel.Application"))

it creates a new, empty instance of excel even though there is already one running. I'm using AutoCAD MEP 2018 and Excel 2010. Is this a bug in VisualLisp or am I doing something wrong? If it's a bug are there any workarounds?

Thanks for any help that can be offered with this.

0 Likes
1,788 Views
7 Replies
Replies (7)
Message 2 of 8

ronjonp
Mentor
Mentor

Have you tried this?

(setq ^ExcelApp (vlax-get-or-create-object "Excel.Application"))
0 Likes
Message 3 of 8

deheylen
Contributor
Contributor

I did try that and I get the same result: a new instance of Excel is created rather than linking to the existing instance.

0 Likes
Message 4 of 8

Sea-Haven
Mentor
Mentor

Have a look at the start of this code. its a bit long winded many lines not just a couple.

 

0 Likes
Message 5 of 8

ronjonp
Mentor
Mentor

@deheylen wrote:

I did try that and I get the same result: a new instance of Excel is created rather than linking to the existing instance.


I cannot replicate this .. I'm also on 2016 version though.

0 Likes
Message 6 of 8

Anonymous
Not applicable

Did you ever find a solution to this? I'm having the same issue on a specific machine. vlax-get-or-create-object ends up creating a new Excel.Application object instead of getting the current one. Both machines are running Office 365 but behave differently. The only difference is, one runs ACAD 2018, and the problematic one runs ACAD 2020. 

0 Likes
Message 7 of 8

Sea-Haven
Mentor
Mentor

I had some other code and it did a check is excel open if not open it will try to find its a variation on the 

(setq *ExcelApp% (vlax-get-or-create-object "Excel.Application"))

 

 

 

: (setq excel-app (vlax-get-or-create-object "excel.application")
(_>      wb-collection (vlax-get excel-app "workbooks"))
#<VLA-OBJECT Workbooks 0000000023950548>
: (vlax-dump-object wb-collection)
; Workbooks 23950548 : 
; Property values :
;   Application (RO) = #<VLA-OBJECT _Application 0000000023951268>
;   Count (RO) = 1

Count will be 0 if no excel open but if no workbooks also may return same value. Nil names. So a double check count /=0 and wb not ""

(vlax-for wb wb-collection
	(princ     (vla-get-fullname wb))
)

 

 

Message 8 of 8

MrJSmith
Advocate
Advocate

I have run into the same issue. In the past, I could typically fix it by restarting my computer (I tend to leave it on for weeks at a time.) However, it seems that won't fix the issue anymore with the latest version of Office 365. Autocad now seems unable to detect that Excel is indeed open when not opening Excel via VLAX commands.

 

This makes it impossible to handle Excel files in an orderly fashion through AutoCAD visual commands and completely ruins many of my Excel handling scripts (or at least requires the user to close all Excel files before running the scripts.)

 

There might be some Microsoft excel security feature that is blocking the information?

0 Likes