?
Solved

Excel Sheet Name

Posted on 2003-02-24
4
Medium Priority
?
308 Views
Last Modified: 2010-04-01
Hi,
I need to read the sheet name of an excel file using vc++.
Is this possible?
Thanks
0
Comment
Question by:RobertParkinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 8008651
There are a whole lot of things that you can do with Excel, but they all require that Excel is installed on the end user's machine.  Look in the .tlh files to start learning.  But either way, here's a way that you can get the name of a worksheet in Excel:

#include <iostream>

// In the #include area,
// import the type library information
#import "C:\Program Files\Microsoft Office\Office\MSO9.DLL" \
  rename("RGB", "OfficeRGB") \
  rename("CopyFile", "OfficeCopyFile") \
  exclude("DocumentProperties") \
  no_namespace

#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files\Microsoft Office\Office\EXCEL9.OLB" \
  rename("DialogBox", "ExcelDialogBox") \
  rename("RGB", "ExcelRGB") \
  rename("CopyFile", "ExcelCopyFile") \
  no_dual_interfaces


// In your function
int main()
{
  CoInitialize(NULL); // Initialize COM
  try
  {
    Excel::_ApplicationPtr excel;
    HRESULT hr = excel.CreateInstance(L"Excel.Application"); // Start excel
    Excel::_WorkbookPtr workbook = excel->Workbooks->Open("c:\\Book1.xls"); // Open the workbook
    Excel::_WorksheetPtr worksheet = workbook->Sheets->Item[1l]; // Get the first worksheet
    std::string worksheet_name = static_cast<char *>(worksheet->Name); // Get the worksheet name
    workbook->Close(); // Close the workbook
    excel->Quit(); // Quit Excel
  }
  catch(_com_error &ce)
  {
    // Error occurred
  }
  CoUninitialize();
  return 0;
}

Good luck!
0
 

Author Comment

by:RobertParkinson
ID: 8009153
thanks mnashadka,
I am having a little trouble converting the string worksheet_name into anything that i can use though.
it won't convert to a CString and i can not use it in an AfxMessageBox to test to see if it is working.
any ideas?

Thanks
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 240 total points
ID: 8009185
I prefer the std::string, which can be converted to a const char * by calling worksheet_name.c_str();  However, you could have just as easily said "CString worksheet_name = " ... since you are converting it to a char *.
0
 

Author Comment

by:RobertParkinson
ID: 8015463
Thanks mnashadka,
That did the trick!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question