LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get a refnum to multiple instances of Excel that were already opened by the user?

This question was asked before here (http://forums.ni.com/t5/LabVIEW/How-do-I-get-a-refnum-to-multiple-open-excel-instances/td-p/1851037) but I didn’t find a solution.

 

I am using ActiveX to control Excel 2010 from my application (LabVIEW 2012).

If I call Open Automation multiple times with 'create a new instance' set to false, a reference to the same instance of Excel is returned. I would like to get a reference to each open Excel instance but I have not found a way to do that yet.

I am including a screen shot showing two open instances of Excel for clarity.

We have multiple monitors so users will often open multiple instances of Excel, typically one instance per monitor.

I would like my program to work even if the user has already opened the Excel file that my program needs to interact with, in the second or higher instance of Excel without having to close the first instance of excel.

0 Kudos
Message 1 of 8
(4,530 Views)

I just tested this using the Report Generation Toolkit (which I much prefer over ActiveX when dealing with Excel) and, surprise, it has the same behavior -- one instance of Excel opening both Workbooks.  I then tried double-clicking on my two Excel files -- the first one brought up Excel and showed me the file, the second one, like LabVIEW, used the same running Version of Excel and opened it in a new Window.

 

It clearly is possible to open two "copies" of the Excel program directly from Windows, but I'm not sure how you would do this from LabVIEW.

 

Bob Schor

0 Kudos
Message 2 of 8
(4,502 Views)

Thanks for checking.

If you want to open two instances of excel from LabVIEW, then try this.

Also this is does not cause me problems because in this case LabVIEW started each instance of Excel and has both application refnums in memory from the start. I can keep both application refnums in memory until I am finished.

But my problem is that the user has opened Excel in advance before running LabVIEW, so how does one get that second refnum?

0 Kudos
Message 3 of 8
(4,456 Views)

I still don’t know how to implement this in LabVIEW but now I know that it is necessary to get it directly from ROT (Running Object Table).

I found a few examples in C# but not sure how to port to LabVIEW.

http://stackoverflow.com/questions/22842023/accessing-excel-application-objects-of-other-excel-insta...

Any pointers on how to do this?

Or better still a working example!

0 Kudos
Message 4 of 8
(4,185 Views)

I hate to necro-post a months-old thread, but I was hoping the original thread owner had found a solution to this issue. DM83, you have any luck? I need to do the exact same thing you're trying to do.

0 Kudos
Message 5 of 8
(3,731 Views)

Unfortunately I didn't have time to pursue the issue myself so I settled for the far less elegant solution of detecting if Excel is open, it is then tell the user to save and close Excel, finally click continue in LabVIEW.

0 Kudos
Message 6 of 8
(3,716 Views)

In my experience using the RGT (which I really like, especially when interfacing with Excel), you must educate your Users to not open, save, or close Workbooks if LabVIEW is trying to manipulate their contents.  Fortunately, losing a multi-sheet Excel Workbook after collecting data for an hour leads to "One-Trial Learning" (I won't do that again!).

 

Bob Schor

0 Kudos
Message 7 of 8
(3,696 Views)

What DM83 tried to do doesn't work like that unfortunately. The OLE32 functions return COM object pointers, not really ..Net objects. In addition a LabVIEW .Net refnum is not the .Net object pointer directly but a LabVIEW specific cookie that identifies the underlying .Net object. So passing a .Net refnum to those functions definitely won't work.

 

What would work is to pass an ActiveX refnum type of the correct class to that parameter and configure it in the Call Library Node as IUnknown Pointer.Then LabVIEW will generate the correct refnum to wrap the ActiveX object and also call the according IUnknown::QueryInterface() for the according IID your refnum has.So far so good. This all works until I try to get to the actual BindToObject() method call, which is grayed out in LabVIEW because of the IID parameter which is a complex parameter that the LabVIEW ActiveX interface doesn't support.

 

And there seems no obvious way to convert the returned ActiveX IRunningObjectTable into an according .Net object refnum either.

 

All in all this looks a lot like having to write a DLL either in C++ (ActiveX) or as .Net assembly to implement this code.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 8 of 8
(3,681 Views)