LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

ActiveX puzzle: How to close/cleanup IWebBrowser2 [Excel] Document?

I think the fundamental question we have is why are you trying to do this through a web browser control? You do not need to use a web browser control to automate Excel. Have you looked at the example that ships with LabVIEW?

0 Kudos
Message 11 of 21
(2,055 Views)

@smercurio_fc wrote:

I think the fundamental question we have is why are you trying to do this through a web browser control? You do not need to use a web browser control to automate Excel. Have you looked at the example that ships with LabVIEW?


I want to embed an Excel spreadsheet in a LabVIEW panel:

1) It's a nice aesthetic to have the spreadsheet in a window instead of popping-up the Excel app.

2) One output of the program is an XLS file - the spreadsheet they will see - and I like the What-You-See-Is-What-You-Get approach.

3) My user likes the appearance of Excel charts (part of the outputted XLS) and my goal is to render an Excel chart much like a LabVIEW chart.

     (Excel will do vertical printing of X-scale values.)

 

When I manipulate the sheet using a seperately obtained reference to Excel, it starts throwing errors once IE loads the sheet (the same problem occurs in VB).

 

Obtaining the sheet from IWebBrowser2.Document allows me to edit without any conflicts.

 

I'm simply asking how to close or release the "Document" object.

0 Kudos
Message 12 of 21
(2,053 Views)

@550nm wrote:

Hi nyc,


@nyc_(is_out_of_here) wrote:

Excel closes for me whether the Excel Open/Close is either true or false. There is not a residual Excel application running in Task Manager.

 

There are other ways to obtain the document properties of Office properties without going through all this gyration, but without knowing what you are trying to achieve it is hard to continue.

 

Good luck! 


The problem comes when trying to use one of those "other ways" to update the sheet while IE is running a second instance of Excel which is also showing the sheet.

Try it.Smiley Wink

 

It's curious that the problem doesn't happen for you - can you change your copy to NOT close the ActiveX Workbook ref?  That should force Excel to stay open.

NI may have changed resource cleanup between 2010 and 2011, keeping the VI running might prevent Excel from closing - for TRUE case - on your platform.

 

Cheers!


 


It dosn't happy for me either way I set the Boolean control.

 

It could be that I have Office 2010. Who knows?

Since I don't see the problem you are having and since I am still not quite sure what you are talking about, I'll leave it at that.

 

 

 

 

0 Kudos
Message 13 of 21
(2,050 Views)

Hi nyc,

      Thank you for trying. Smiley Happy

 

Cheers!

0 Kudos
Message 14 of 21
(2,043 Views)

@550nm wrote:

Hi nyc,

      Thank you for trying. Smiley Happy

 

Cheers!


Since it is usually easier to debug these things in .NET, I fired up Visual Basic 2010 Express.

Put a Web Browser control on it, and Navigate to an Excel file on my hard drive.

Then I put a breakpoint to check what IWebBrowser2's Document property was that you have in your LabVIEW VI.

You have it being converted from a Variant to an Excel workbook.

In VB.NET, Document property is Nothing/Null.

I strongly doubt you will be able to manipulate the Excel file using that object in LabVIEW.

 

 

0 Kudos
Message 15 of 21
(2,036 Views)

@nyc_(is_out_of_here) wrote:

You have it being converted from a Variant to an Excel workbook.

In VB.NET, Document property is Nothing/Null.

I strongly doubt you will be able to manipulate the Excel file using that object in LabVIEW. 


Hi nyc,

      I'm excited that you are able to work with VB so easily - if you can find the solution there then perhaps it will port to LabVIEW.

This method is already working, by the way - spreadsheet contents update very nicely.  If you will notice my example obtains the Workbook name and (now) the Application name - rather difficult to explain if Document is null. Smiley Wink

 

If Document is null for you, perhaps you're not waiting for the page to load first.

 

Thanks/Cheers.

0 Kudos
Message 16 of 21
(2,031 Views)

@550nm wrote:

@nyc_(is_out_of_here) wrote:

You have it being converted from a Variant to an Excel workbook.

In VB.NET, Document property is Nothing/Null.

I strongly doubt you will be able to manipulate the Excel file using that object in LabVIEW. 


Hi nyc,

      I'm excited that you are able to work with VB so easily - if you can find the solution there then perhaps it will port to LabVIEW.

This method is already working, by the way - spreadsheet contents update very nicely.  If you will notice my example obtains the Workbook name and (now) the Application name - rather difficult to explain if Document is null. Smiley Wink

 

If Document is null for you, perhaps you're not waiting for the page to load first.

 

Thanks/Cheers.



No, I am waiting for the Excel file to load first by waiting on the DocumentCompleted event.

 

In .NET world, WebBrowser's Document proprerty is defined as System.Windows.Forms.HtmlDocument

which probably explains why it is Nothing/Null since the Excel document isn't a HTML document.

When I change my code to Navigate to Test.html, then Document is no longer Nothing.

 

 

The fact that Excel remains running in the background isn't unexpected.

The following C# code shows the effort the programmer makes to close down that background Excel. http://www.codeproject.com/Articles/15760/How-to-Integrate-Excel-in-a-Windows-Form-Applicati

Perhaps you can convert it into LabVIEW code?

 

 

 

 

Message 17 of 21
(2,024 Views)

@nyc_(is_out_of_here) wrote:

The fact that Excel remains running in the background isn't unexpected.

The following C# code shows the effort the programmer makes to close down that background Excel. http://www.codeproject.com/Articles/15760/How-to-Integrate-Excel-in-a-Windows-Form-Applicati

Perhaps you can convert it into LabVIEW code?


YES! This is exactly what I want to do!

 

The key seems to be this section:

QUOTE:

// Quit Excel and clean up.
        if(m_Workbook!=null)
        {
            m_Workbook.Close(true,Missing.Value,Missing.Value);
            System.Runtime.InteropServices.Marshal.ReleaseComObject
                                    (m_Workbook);
            m_Workbook=null;
        }

QUOTE_END

 

"m_Workbook.Close()" is just invoking the Workbook Close method - no problem there

"m_Workbook=null" is (i think) equivilant to "Close Reference" - again, no problem

 

Q. How to perform System.Runtime.InteropServices.Marshal.ReleaseComObject()?

 

I've explored the (NET) System.Runtime.InteroptServices list/library, but LabVIEW seems to be missing some key functions, like "Marshal" and "Finish".

 

With your familiarity with ActiveX, maybe you can figure out how to "Marshal.ReleaseComObject()" with LabVIEW?

 

If LabVIEW isn't capable of performing the Marshal.Release or equivilent, I'll probably write a DLL to do it.

 

Thanks/Cheers!

 

 

0 Kudos
Message 18 of 21
(2,016 Views)

Perhaps you can just kill the Excel application process running in the Task Manager.

http://digital.ni.com/public.nsf/allkb/80432B061C5A4C888625705400737BE8

0 Kudos
Message 19 of 21
(2,013 Views)

Hi nyc,


@nyc_(is_out_of_here) wrote:

Perhaps you can just kill the Excel application process running in the Task Manager.

http://digital.ni.com/public.nsf/allkb/80432B061C5A4C888625705400737BE8


Thanks nyc.  I saw this suggestion on a VB forum a week ago.  I'd rather not use such a brute force approach, besides, it's not quite so easy.  There may be multiple Excels open and we'd need to close the one associated with the IWebBrowser2 control.  It requires determining the process ID of the specific instance of Excel and terminating via process ID.

 

I'm still hoping there's a more elegant way to solve this.

Thanks/Cheers.

0 Kudos
Message 20 of 21
(2,000 Views)