473,382 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to read late bound Excel worksheets collection properties?

Hello,

I am using late binding in a managed VC++ .NET 2003 application. I used KB
302902 (for C#) as a starting point and converted it to managed C++. I built
a managed class that is intantiated by a form. The class can instantiate
Excel, open a new workbook, add a new worksheet, get a range, and put some
data in it. So, I have basically just duplicated what KB 302902 does, except
I am working out of a managed C++ class.

The problem comes when I wish to open an existing workbook and find what
worksheets are in it.

In the first read attempt, the form used an OpenFileDialog to let the user
select which workbook to open. The class successfully instantiated Excel,
added the workbook received from the OpenFileDialog to the workbooks
collection, and made Excel visible, using late binding. Good.

In the second read attempt, the class gets the worksheets collecton:

// Declarations in the class header file.
System::Object * objBook_Late;
System::Object * objSheets_Late;

// Get the worksheets collection.
objSheets_Late = objBook_Late->GetType()->InvokeMember( "Worksheets",
System::Reflection::BindingFlags::GetProperty,
NULL, objBook_Late, NULL );

I think the above is OK. One of the worksheets collection properties is
Count. What I wish to do next is get the count, then iterate through the
z-list and record the worksheet names in an array. (As you can see, I did
not change the KB 302902 object names...I am just learning here.)

Since there is an object objSheets_Late, I tried to use it to get the count.
I used both GetProperty and GetField for BindingFlags. I could get a return
into another object with GetProperty, yet I still am not sure how to find
the count in the new object.

Can someone point me to an information source on how to extract the
properties from a late bound object such as objSheets_Late? Two things in
particular. 1. Extract a value such as Count. 2. Iterate through the z-list
to extract the corresponding worksheet names.

Thanks,

Dan
Apr 20 '06 #1
1 3627
I have answered my question.

// Declarations in the class header file.
System::Object * worksheetCount;
System::String * worksheetNames[];

// Get the count of worksheets in the workbook.
worksheetCount = new Object;
worksheetCount = objSheets_Late->GetType()->InvokeMember( "Count",
System::Reflection::BindingFlags::GetProperty, NULL,
objSheets_Late, NULL );
// Store the count of worksheets in a local variable.
// To do this, the count must be 'unboxed' via a dynamic cast.
// The source for how to do this was 'Microsoft Visual C++ .NET Step by
Step: Version 2003',
// Chapter 25, in the section 'Unboxing'.
int wsCount = *dynamic_cast<__box int*>(worksheetCount);
// Get the names of the worksheets. The worksheet count starts with 1.
this->worksheetNames = new System::String * [wsCount+1];
System::Object * wsName;
for(int i = 1; i <= wsCount; i++)
{
Parameters = new Object * [1];
Parameters[0] = __box(i);
objSheet_Late = objSheets_Late->GetType()->InvokeMember( "Item",
System::Reflection::BindingFlags::GetProperty, NULL,
objSheets_Late, Parameters );
wsName = new Object;
wsName = objSheet_Late->GetType()->InvokeMember( "Name",
System::Reflection::BindingFlags::GetProperty, NULL,
objSheet_Late, NULL );
// Store the name.
System::String * wsTemp = dynamic_cast<System::String*>(wsName);
this->worksheetNames[i] = wsTemp;
}

Now I am able to access the range object on any available worksheet as
needed.

The problem I had was with understanding how to use the C++, not with the
Excel object model.
Apr 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
1
by: Randall Arnold | last post by:
I'm converting a vbscript program to vb.net. Witht he exception of .net idiosyncrasies, most of it is working well with the same code. My only problem is that some properties and methods are...
1
by: Esmail Bonakarian | last post by:
Greetings all, What is the best way to access specific records in an Excel file? I have an Excel file, I want to randomly and repeatedly (maybe around up to 50 times) draw some rows of data...
4
by: Seok Bee | last post by:
Dear Experts, I have created a script to extract the Event Logs from the system into an excel sheet. The logs are separated into 2 worksheets (Application Log and System Log). After this excel...
3
by: implicate_order | last post by:
Greetings, I'm new to python and am in the process of writing a script to parse some CSV data, spread it across multiple Excel worksheets and then generate charts. I searched the internet to...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
3
by: mike11d11 | last post by:
I was able to create three worksheets in my workbook, but when I go to add the 4th I get an Invalid Index error. I must be leaving something out to when adding 4 or more sheets. Thanks Dim...
2
by: GS | last post by:
I have installed the ms PIA for ofc XP, and followed the article http://support.microsoft.com/kb/247412/ trying to paste into a worksheet However I got late binding not allowed errors .......
0
by: sg2611 | last post by:
I am getting a warning in my VB.Net application Warning Late bound resolution; runtime errors could occur. for the following code Private Function CreateReportHeader(ByRef ExcelApp As...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.