IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API, COM et SDKs Delphi Discussion :

Automation Excel


Sujet :

API, COM et SDKs Delphi

  1. #1
    cgo
    cgo est déconnecté
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 27
    Points : 16
    Points
    16
    Par défaut Automation Excel
    Bonjour à tous,

    J'ai un p'tit souci, Je n'arrive pas à insérer toutes mes données dans les différentes cellules.

    Lorsque j'exécute mon code cela me donne l'erreur suivante:
    "Le Variant ne référence pas un objet automation".

    J'ai fait le test d'insérer une valeur à la fois, indépendamment les unes des autres et là, çà fonctionne, c'est quand je veux le faire d'un seul bloc que çà ne fonctionne pas.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
     
     
    uses ComObj;
     
    var
      FCompteComm: TFCompteComm;
      OleExcel: Variant;
      XlWorkBooks,XlWorkBook: Variant;
      XlWorkSheet,XlCell: Variant;
      aFileName,aSheetName,aRange: AnsiString;
    implementation
     
    {$R *.DFM}
     
    uses UGlobal;
     
    procedure TFCompteComm.FormCreate(Sender: TObject);
    begin
     
         ShowModal;
     
    end;
     
    procedure TFCompteComm.BtnOkClick(Sender: TObject);
    var
       DateDeb: TDate;
       changement: Boolean;
       compteur:integer;
       precedent: string;
    begin
     
         aFileName:='d:\automation_excel\compte_comm.xls';
         //aSheetName:='Feuil1';
         DateDeb:=EncodeDate(StrToInt(EAnnee.Text),StrToInt(EMois.Text),1);
     
         QCompteComm.close;
         QCompteComm.Params[0].AsInteger:=Site;
         QCompteComm.Params[1].AsDate   :=EncodeDate(StrToInt(EAnnee.Text),StrToInt(EMois.Text),1);
         QCompteComm.Params[2].AsDate   :=DateEcheance(1,StrToInt(EMois.Text),StrToInt(EAnnee.Text));
         QCompteComm.open;
     
         OleExcel:=CreateOleObject('Excel.application');
         XlWorkBooks:=OleExcel.WorkBooks;
         XlWorkBook :=XlWorkBooks.open(aFilename);
     
         compteur:=1;
     
         QCompteComm.First;
         precedent:=QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString;
     
         while not QCompteComm.Eof do
         begin
     
              if changement then
              begin
     
                   XlWorkSheet:=XlWorkBook.WorkSheets[aSheetName];
                   XlWorkSheet.PageSetup.CenterHeader:= ' Secteur ' + QCompteComm.FieldByName('VTSG_CLE').AsString + ' ' +  QCompteComm.FieldByName('VTSG_LIBEL').AsString + ' ' + QCompteComm.FieldByName('PAST_SOCIETE').AsString + #13 +
                                                        'Chiffre d''affaire par client ' + FormatDateTime('mmmm',DateDeb) + ' ' + EAnnee.text;
                   XlWorkSheet.PageSetup.CenterFooter:= DateToStr(date);
     
                   changement:=false;
     
              end;
     
              if QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString = precedent then
              begin
     
                   aRange:='A' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString;
     
                   aRange:='B' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('VTCLI_NOM').AsString;
     
                   aRange:='C' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('PACPV_CP').AsString; 
     
                   aRange:='D' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('PACPV_LIBEL').AsString;
     
                   aRange:='E' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:='Facture du ' + QCompteComm.FieldByName('VTFACTURE_DATFAC').AsString  + ' n° ' + QCompteComm.FieldByName('VTFACTURE_CLE').AsString;
     
                   aRange:='F' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('VTFACTURE_TOTHT').AsString;
     
                   inc(compteur);
     
              end;
     
              if QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString <> precedent then
              begin
     
                   inc(compteur);
     
                   aRange:='A' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString;
     
                   aRange:='B' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('VTCLI_NOM').AsString;
     
                   aRange:='C' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('PACPV_CP').AsString;
     
                   aRange:='D' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('PACPV_LIBEL').AsString;
     
                   aRange:='E' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:='Facture du ' + QCompteComm.FieldByName('VTFACTURE_DATFAC').AsString + ' n° ' + QCompteComm.FieldByName('VTFACTURE_CLE').AsString;
     
                   aRange:='F' + IntToStr(Compteur);
                   XlCell:=XlWorkSheet.Range[aRange];
                   XlCell.Value:=QCompteComm.FieldByName('VTFACTURE_TOTHT').AsString;
     
                   inc(compteur);
                   precedent:=QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString;
     
              end;
     
              QCompteComm.Next;
     
         end;
     
         OleExcel.Visible:=true;
     
    end;
     
    procedure TFCompteComm.FormClose(Sender: TObject;
      var Action: TCloseAction);
    begin
     
         OleExcel.Quit;
         OleExcel:=Unassigned;
     
    end;

  2. #2
    Membre régulier
    Inscrit en
    Avril 2002
    Messages
    103
    Détails du profil
    Informations forums :
    Inscription : Avril 2002
    Messages : 103
    Points : 120
    Points
    120
    Par défaut
    Salut,

    j'ai déjà eu ce genre d'erreur dans le cas d'une affectation Unassigned en double... la première marche (normal), pas la deuxième (normal aussi...)

    dans ton cas où se produit l'erreur ??

    @+
    Ce n'est pas parce qu'on pédale dans la semoule, qu'on est sûr de manger du couscous... (anonyme)

  3. #3
    cgo
    cgo est déconnecté
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 27
    Points : 16
    Points
    16
    Par défaut
    Mon erreur se passe juste après le bloc en rouge.


    if QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString = precedent then
    begin

    aRange:='A' + IntToStr(Compteur);
    XlCell:=XlWorkSheet.Range[aRange];
    XlCell.Value:=QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString;

    aRange:='B' + IntToStr(Compteur);
    XlCell:=XlWorkSheet.Range[aRange];
    XlCell.Value:=QCompteComm.FieldByName('VTCLI_NOM').AsString;

    aRange:='C' + IntToStr(Compteur);
    XlCell:=XlWorkSheet.Range[aRange];
    XlCell.Value:=QCompteComm.FieldByName('PACPV_CP').AsString;


    aRange:='D' + IntToStr(Compteur);
    XlCell:=XlWorkSheet.Range[aRange];
    XlCell.Value:=QCompteComm.FieldByName('PACPV_LIBEL').AsString;

    aRange:='E' + IntToStr(Compteur);
    XlCell:=XlWorkSheet.Range[aRange];
    XlCell.Value:='Facture du ' + QCompteComm.FieldByName('VTFACTURE_DATFAC').AsString + ' n° ' + QCompteComm.FieldByName('VTFACTURE_CLE').AsString;

    aRange:='F' + IntToStr(Compteur);
    XlCell:=XlWorkSheet.Range[aRange];
    XlCell.Value:=QCompteComm.FieldByName('VTFACTURE_TOTHT').AsString;

    inc(compteur);

    end;

  4. #4
    Membre régulier
    Inscrit en
    Avril 2002
    Messages
    103
    Détails du profil
    Informations forums :
    Inscription : Avril 2002
    Messages : 103
    Points : 120
    Points
    120
    Par défaut
    Re,

    je suis très étonné... tu es sûr que c'est à cet endroit là que ça plante ?

    ces lignes sont identiques aux précédentes donc il n'y a pas de raisons qu'une erreur se produise là... à moins que la colonne C n'existe pas... et encore...

    place des ShowMessage entre chaque bloc avec renvoi du range (par exemple...). Il se peut qu'il y ait un décalage entre le mess d'erreur et l'erreur...

    tiens nous au courant...

    @+
    Ce n'est pas parce qu'on pédale dans la semoule, qu'on est sûr de manger du couscous... (anonyme)

  5. #5
    cgo
    cgo est déconnecté
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 27
    Points : 16
    Points
    16
    Par défaut
    Merci beaucoup Amenofis pour ton aide.
    J'ai trouvé la solution a mon problème. Je ne sais pas si c'est la meilleure mais en tout cas, çà marche.


    J'ai remplacé
    aRange:='A' + IntToStr(Compteur);
    XlCell:=XlWorkSheet.Range[aRange];
    XlCell.Value:=QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString;
    Par
    aRange:='A' + IntToStr(Compteur);
    OleExcel.Range[aRange].Formula:=QCompteComm.FieldByName('VTCLI_MOTDIREC').AsString;
    Et là çà marche.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. automation Excel: sélection de cellule
    Par jarod_bx dans le forum Access
    Réponses: 3
    Dernier message: 03/11/2005, 15h33
  2. [Automation Excel] ajuster des cellules excel
    Par willich dans le forum Access
    Réponses: 4
    Dernier message: 10/10/2005, 10h04
  3. petit probleme avec automation Excel
    Par Tempotpo dans le forum API, COM et SDKs
    Réponses: 6
    Dernier message: 05/08/2005, 08h57
  4. Automation Excel
    Par GoldenEye dans le forum MFC
    Réponses: 2
    Dernier message: 24/05/2005, 15h11
  5. automation excel : comment indiquer la feuille de travail ?
    Par mat_lefebvre dans le forum Access
    Réponses: 2
    Dernier message: 04/12/2003, 15h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo