Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 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

24 Experts available now in Live!

Get 1:1 Help Now