Solved

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

Posted on 2011-03-23
2
241 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now