Solved

How To Rename A Spreadsheet And Save The Worbook To A Specific File Location

Posted on 2011-03-23
2
247 Views
Last Modified: 2012-05-11
I have created and opened a new Excel spreadsheet using COM in Visual Studio 2010 C++.  How can I rename a sheet in the workbook and how can I save the workbook to a specific location?  I made this worth 500 because I put 2 questions in here but I'm pretty sure they are easy answers.  I just don't know the correct method to use or where to look to find a list of methods available through COM for Excel.

XL.CreateInstance(L"Excel.Application");
XL->Visible = true;
XL->Workbooks->Add();
Excel::_WorkbookPtr p_workbook=XL->ActiveWorkbook;
Excel::_WorksheetPtr pSheet = p_workbook->ActiveSheet;

Open in new window

0
Comment
Question by:rgrguric
  • 2
2 Comments
 

Accepted Solution

by:
rgrguric earned 0 total points
ID: 35235083
Looks like I'm gonna answer my own question!  

To rename a sheet in Excel through C++ COM:

Excel::_WorksheetPtr hosenSheet(XL->Worksheets->Item[7]); //Stores ptr to Sheet 7 as chosenSheet
chosenSheet->Name = "Compat Rules"; //Renames Sheet7 to Compat Rules

Open in new window


And finally to save the workbook to a specific location:

char outputPath[200];
char* wndwsUserID = getenv("USERNAME"); //Gets current user logon name for windows
strcpy(outputPath,"C:\\Documents and Settings\\");
strcat(outputPath, wndwsUserID); 
strcat(outputPath,"\\Desktop\\test.xls");

Open in new window

0
 

Author Closing Comment

by:rgrguric
ID: 35235093
No one else posted any comments so I work on this all weekend and came up with a solution!
0

Featured Post

Independent Software Vendors: 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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

680 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