ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Working with Excel 2003 from a Visual C++ program (https://www.excelbanter.com/excel-programming/437163-working-excel-2003-visual-c-program.html)

zm01

Working with Excel 2003 from a Visual C++ program
 
I am quite new to programming. Despite that I have to write a C++ program
that opens an .xls workbook, selects a specified worksheet and puts data in
specified cells of the worksheet. I have found a document in the Internet
that describes how to use the Excel COM model. Based on this I created a
program that is able o open a specified .xls workbook and put data in
specified cells in the active sheet. However I do not know how to select a
specified worksheet so that I do not put the data in the worksheet that ust
happens to be active when opening the workbook. How to do that?

Here is the code that does what I described:

CoInitialize(NULL);
Excel::_ApplicationPtr xl;
xl.CreateInstance(L"Excel.Application");
xl-Visible = true;
xl-Workbooks-Open(L"C:\\Documents and Settings\\WRO00724\\my
documents\\Visual Studio
2008\\Projects\\Project1\\Excel3\\Excel3\\Example. xls");
Excel::_WorksheetPtr pSheet = xl-ActiveSheet;
Excel::RangePtr pRange = pSheet-Cells;
pRange-Item[2][1] = 0.86;
pRange-Item[3][1] = "test";

What program lines should I include in order to be able to specify the
particular worksheet the data have to go to?

zm01

Working with Excel 2003 from a Visual C++ program
 

I have finally found a solution:

Excel::_WorksheetPtr myWrks(xl-Worksheets-Item["Sheet2"]);
myWrks-Activate();


So my program looks now this way:

CoInitialize(NULL);
Excel::_ApplicationPtr xl;
xl.CreateInstance(L"Excel.Application");
xl-Visible = true;
xl-Workbooks-Open(L"C:\\Documents and Settings\\WRO00724\\my
documents\\Visual Studio
2008\\Projects\\Project1\\Excel3\\Excel3\\Example. xls");
Excel::_WorksheetPtr myWrks(xl-Worksheets-Item["Sheet2"]);
myWrks-Activate();
Excel::_WorksheetPtr pSheet = xl-ActiveSheet;
Excel::RangePtr pRange = pSheet-Cells;
pRange-Item[2][1] = 0.86;
pRange-Item[3][1] = "test";



"zm01" wrote:

I am quite new to programming. Despite that I have to write a C++ program
that opens an .xls workbook, selects a specified worksheet and puts data in
specified cells of the worksheet. I have found a document in the Internet
that describes how to use the Excel COM model. Based on this I created a
program that is able o open a specified .xls workbook and put data in
specified cells in the active sheet. However I do not know how to select a
specified worksheet so that I do not put the data in the worksheet that ust
happens to be active when opening the workbook. How to do that?

Here is the code that does what I described:

CoInitialize(NULL);
Excel::_ApplicationPtr xl;
xl.CreateInstance(L"Excel.Application");
xl-Visible = true;
xl-Workbooks-Open(L"C:\\Documents and Settings\\WRO00724\\my
documents\\Visual Studio
2008\\Projects\\Project1\\Excel3\\Excel3\\Example. xls");
Excel::_WorksheetPtr pSheet = xl-ActiveSheet;
Excel::RangePtr pRange = pSheet-Cells;
pRange-Item[2][1] = 0.86;
pRange-Item[3][1] = "test";

What program lines should I include in order to be able to specify the
particular worksheet the data have to go to?



All times are GMT +1. The time now is 05:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com