probleme d'ouverture d'un fichier excel
Bonjour,
Je souhaite ouvrire un fichier excel déja existant afin de modifier des cellules.
Voici mon programme:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <utilcls.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
extern PACKAGE wchar_t * __fastcall StringToOleStr(const AnsiString Source);
TForm1 *Form1;
Variant vMSExcel, vXLWorkbooks, vXLWorkbook, vWorksheet,vFileName;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
vMSExcel = Variant::CreateObject("Excel.Application");
vMSExcel.OlePropertySet("Visible", true);
vXLWorkbooks = vMSExcel.OlePropertyGet("Workbooks");
vXLWorkbooks= vXLWorkbooks.OleFunction("Add");
vXLWorkbooks = vXLWorkbooks .OlePropertyGet("Worksheets",1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
vMSExcel.OleFunction("Quit");
vMSExcel = Unassigned;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
Variant vValue, vRange, vCell;
AnsiString s;
s=Edit1->Text;
vCell = vWorksheet.OlePropertyGet("Cells",4,1);
vValue = s.c_str();
vCell.OlePropertySet("Value", vValue);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant vValue, vRange, vCell;
AnsiString s;
s=Edit1->Text;
vCell = vWorksheet.OlePropertyGet("Cells",1,1);
vValue = s.c_str();
vCell.OlePropertySet("Value", vValue);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
vMSExcel = Variant::GetActiveObject("Excel.Application");
vMSExcel.OlePropertySet("Visible", true);
vFileName= "C:\\Classeur.xls";
vXLWorkbooks = vMSExcel.OlePropertyGet("Workbooks");
vXLWorkbooks= vXLWorkbooks.OleFunction("Open",vFileName);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
vXLWorkbook.OleProcedure("Save");
} |
Mon programme ouvre bien lorsque que jappuye sur le bouton 2 (creation d'un nouveau fichier) mais il me met une erreur lorsque que je souhaite ouvrir un fichier déja existant (bouton 6) "Opération non disponible".
1) Cependant lorsque je clique sur le bouton 2,ensuite je ferme la page excel je peu cliquer sur le bouton 6 et sans erreur...
2) De plus si je veu modifier une cellule d'un fichier existant il me met une erreur "OLE error 800A01A8"
Merci d'avance