Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ActiveX Template Library. How do I connect to excel spreadsheet???

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm trying to open an excel spreadsheet using my application. I have a form with ExcelApplication, ExcelWorkbook, and ExcelWorksheet objects placed on it.

I can launch the excel application from my application but I'm having trouble connecting to a particular spreadsheet. How do I connect to a particular spreadsheet????

My code is shown below (the code is incomplete), can anyone give me an example of connecting to a file (excel NOT WORD!!) ???? :
Code:
void __fastcall TCleaningToolForm::Button_OpenExcelFileClick(TObject *Sender)
{
        PrepareExcel(); // launch excel

        // get file name from user
        if(OpenExcelFileDialog->Execute())
        {
                OleVariant FileName = OpenExcelFileDialog->FileName;

                // open excel file
                ExcelWorkbook1->ConnectTo(ExcelApplication1->Workbooks->Open(

        }
}
 
My code now makes an attempt at connecting to the excel file but displays an error message. Can anyone tell me where I'm going wrong?

Code:
void __fastcall TCleaningToolForm::Button_OpenExcelFileClick(TObject *Sender)
{

        PrepareExcel(); // launch excel

        // get file name from user
        if(OpenExcelFileDialog->Execute())
        {
                //OleVariant FileName = OpenExcelFileDialog->FileName;
                wchar_t  FileName[255];
                (OpenExcelFileDialog->FileName).WideChar(FileName, 255);

                //Application->MessageBoxA
                ShowMessage(FileName);
                // open excel file
                //ExcelWorksheet1->ConnectTo(ExcelApplication1->
                TVariantInParam t;
                t = EmptyParam;

                ExcelApplication1->Workbooks->Open(FileName,t,t,t,t,t,t,t,t,t,t,t,t,LOCALE_SYSTEM_DEFAULT);
        }

}
 
Did you ever find an answer to your question on how to open a particular spreadsheet using the TExcelApplication component?

If not...

In my program I built a drop-down list of the visible worksheets and then when the user chose a worksheet from the list I opened it using the following code:

Excel_2k::SheetsPtr sheets;
_WorksheetPtr ws;
TVariant numSheet;

numSheet =
(int)SourceWorkSheet_ComboBox->Items->Objects[ SourceWorkSheet_ComboBox->ItemIndex ];
sheets = ExcelApplication1->get_Sheets();
ws = sheets->get_Item( numSheet );

I filled the drop-down list as follows:
Excel_2k::SheetsPtr sheets;
_WorkbookPtr wb;
_WorksheetPtr ws;
AnsiString StrValue, ActiveSheetName;
int i, j, Sheet_Count;

ws = ExcelApplication1->ActiveSheet;
ActiveSheetName = ws->get_Name();

sheets = wb->get_Sheets();
Sheet_Count = sheets->Count;

// Worksheets are indexed from 1, not 0
// J counts the number of visible sheets, starting from 0
for( i = 1, j = 0; i <= Sheet_Count; ++i ) {
ws = sheets->get_Item( (TVariant)i );
/* -1 is not Excel_2k::xlVeryHidden or False */
if( ws->get_Visible() == -1 ) {
StrValue = ws->get_Name();
SourceWorkSheet_ComboBox->Items->AddObject( StrValue, (TObject *)i );
// Select the active worksheet's name
if( ActiveSheetName == StrValue )
SourceWorkSheet_ComboBox->ItemIndex = j;
++j;
}
}

I use the ws->get_Name() method to get the name of the worksheet. You can also &quot;walk&quot; through the spreadsheets and check each one's name.

I found that the WROX Excel 2000 VBA book was very good for figuring out what the different Excel properties and methods did, but you can also use the help on and the help in the Excel 2000 VB editor and get pretty far.

Good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top