Problème automation excel

Résolu
gregorian Messages postés 57 Date d'inscription lundi 18 avril 2005 Statut Membre Dernière intervention 24 mars 2011 - 10 mai 2007 à 12:39
michelatoutfox Messages postés 828 Date d'inscription mardi 5 octobre 2004 Statut Membre Dernière intervention 7 mai 2013 - 14 mai 2007 à 13:18
Bonjour à tous,
J'ai un petit problème de mise en page excel.
Dans un application, je récupère des données dans un curseur qui doit alors ouvrir un fichier excel. Tout ca, ca va
Maintenant, on me demande de quadriller automatiquement ce fichier.
Et la j'ai un problème sur la sélection.

Sur le net, j'ai trouver le code suivant:
 loSheet.Range['A1','E15'].select

    with Selection.borders(xlEdgeLeft)

        .LineStyle=xlContinuous

        .Weight=xlThin

        .ColorIndex=xlAutomatic

    endwith

Mais si je fais ca, il me sort le message d'erreur suivant: définition de classe EXCEL.APPLICATION.SELECTION introuvable
Dans mon code, je rajoute donc loSel = CREATEOBJECT("Excel.Application.Selection")
Mais cela ne semble pas être ca car il me renvoie le même erreur.

Qqun peut il m'aider ?
PS j'ai déjà été voir l'article de Mike Ganon sur atout fox mais il ne donne pas d'exemple sur la selection...
ou plutot, il part d'une feuille et pas d'une sélection range de cell.

Merci de votre aide.

LOCAL loExcel, loBook, loSheet, lnLine
    
    loExcel = CREATEOBJECT("Excel.Application")
    loBook = loExcel.workbooks.add()
    loSheet = loBook.worksheets(1)    
    lnLine = 2
    
    loSheet.Range['A1','A1'].EntireColumn.ColumnWidth = 15
    loSheet.Range['B1','B1'].EntireColumn.ColumnWidth = 10
    loSheet.Range['C1','C1'].EntireColumn.ColumnWidth = 40
    loSheet.Range['D1','D1'].EntireColumn.ColumnWidth = 30
    loSheet.Range['E1','E1'].EntireColumn.ColumnWidth = 10
    loSheet.Range['F1','F1'].EntireColumn.ColumnWidth = 10
    loSheet.Range['G1','G1'].EntireColumn.ColumnWidth = 10
    loSheet.Range['H1','H1'].EntireColumn.ColumnWidth = 20
    loSheet.Range['I1','I1'].EntireColumn.ColumnWidth = 20
    loSheet.Range['J1','J1'].EntireColumn.ColumnWidth = 20
    loSheet.Range['K1','K1'].EntireColumn.ColumnWidth = 20
    loSheet.Range['L1','L1'].EntireColumn.ColumnWidth = 20
    
    loSheet.cells(1,1).Value = "Ref"
    loSheet.cells(1,2).Value = "PCB"    loSheet.cells(1,3).Value iif(snLangue 1,"Description","Bescherming")    loSheet.cells(1,4).Value iif(snLangue 1,"Photo","Picture")    loSheet.cells(1,5).Value iif(snLangue 1,"Prix","Prijs")
    
    if snCont = 1 && si on ne demande pas les infos container, on ne crée pas les colonnes        loSheet.cells(1,6).Value iif(snLangue 1,"prix container HC","Cont. prijs HC")        loSheet.cells(1,7).Value iif(snLangue 1,"prix container 20P","Cont. prijs 20P")        loSheet.cells(1,8).Value iif(snLangue 1,"prix container 40P","Cont. prijs 40P")
        loSheet.cells(1,9).Value = "Qty HC"
        loSheet.cells(1,10).Value = "Qty 20p"
        loSheet.cells(1,11).Value = "Qty 40p"
        loSheet.cells(1,12).Value = "usine"
    endif
    
    select c_result
    go top
    do while !EOF()
        loSheet.cells(lnLine,1).value = c_result.ref
        loSheet.cells(lnLine,2).value = c_result.pcb
        loSheet.cells(lnLine,3).value = c_result.nom
        *if file(gcDataDir + "PICTURES\ART" + alltrim(c_result.ref)+".JPEG")
        *    loSheet.range['D3','D3'].select
        *    loSheet.pictures.insert(gcDataDir + "PICTURES\ART" + alltrim(c_result.ref)+".JPG")
        *endif
        *loSheet.cells(lnLine,4).value = "photo"
        loSheet.cells(lnLine,5).value = c_result.prix
        
        if snCont = 1 && si on ne demande pas les infos container, on ne rempli pas les colonnes            loSheet.cells(lnLine,6).value iif(snCont 1,c_result.prix_conthc,"")            loSheet.cells(lnLine,7).value iif(snCont 1,c_result.prix_cont20,"")            loSheet.cells(lnLine,8).value iif(snCont 1,c_result.prix_cont40,"")            loSheet.cells(lnLine,9).value iif(snCont 1,c_result.qty_contHC,"")            loSheet.cells(lnLine,10).value iif(snCont 1,c_result.qty_cont20,"")            loSheet.cells(lnLine,11).value iif(snCont 1,c_result.qty_cont40,"")        
            loSheet.cells(lnLine,12).value = c_result.c_usine
        endif
        lnLine = lnLine + 1
        skip
        loop
    enddo
    loExcel.visible = .T.
    
    ** Quadrillage du rapport:
    
    ** Récupération de la dernière case en bas à droite utilisée
*!*        if snCount = 1
*!*            lcMonCell = "L"+str(lnLine)
*!*            messagebox(lcMonCell)
*!*        else
*!*            lcMonCell = "E"+str(lnLine)
*!*            messagebox(lcMonCell)
*!*        endif
    
    loSheet.Range['A1','E15'].select
    with loSel.borders(xlEdgeLeft)
        .LineStyle=xlContinuous
        .Weight=xlThin
        .ColorIndex=xlAutomatic
    endwith
    
    
    
    release loSheet, loBook, loExcel
    
ENDIF

4 réponses

Mike Gagnon Messages postés 381 Date d'inscription vendredi 15 octobre 2004 Statut Membre Dernière intervention 24 octobre 2013 2
13 mai 2007 à 14:13
Je ne suis pas sur si c'est ceci que tu recherche (J'ai enleve le curseur)

#Define xlEdgeLeft    7
#Define xlContinuous    1
#Define xlThin    2
#Define xlAutomatic    -4105
#define xlNone 0
#define xlDiagonalDown  5
#define xlEdgeBottom  9
#define xlDiagonalUp  6
#define xlEdgeTop  8
#define xlEdgeRight  10
#define xlInsideHorizontal  12
#define xlInsideVertical  11
#define xlDiagonalDown  5
#define xlDiagonalUp  6
#define xlEdgeBottom  9
#define xlEdgeLeft  7
#define xlEdgeRight  10
#define xlEdgeTop  8
Local loExcel, loBook, loSheet, lnLine
snLangue = 1
snCont = 1
loExcel = Createobject("Excel.Application")
loBook = loExcel.workbooks.Add()
loSheet = loBook.worksheets(1)
lnLine = 2

loSheet.Range['A1','A1'].EntireColumn.ColumnWidth = 15
loSheet.Range['B1','B1'].EntireColumn.ColumnWidth = 10
loSheet.Range['C1','C1'].EntireColumn.ColumnWidth = 40
loSheet.Range['D1','D1'].EntireColumn.ColumnWidth = 30
loSheet.Range['E1','E1'].EntireColumn.ColumnWidth = 10
loSheet.Range['F1','F1'].EntireColumn.ColumnWidth = 10
loSheet.Range['G1','G1'].EntireColumn.ColumnWidth = 10
loSheet.Range['H1','H1'].EntireColumn.ColumnWidth = 20
loSheet.Range['I1','I1'].EntireColumn.ColumnWidth = 20
loSheet.Range['J1','J1'].EntireColumn.ColumnWidth = 20
loSheet.Range['K1','K1'].EntireColumn.ColumnWidth = 20
loSheet.Range['L1','L1'].EntireColumn.ColumnWidth = 20

loSheet.cells(1,1).Value = "Ref"
loSheet.cells(1,2).Value = "PCB"loSheet.cells(1,3).Value Iif(snLangue 1,"Description","Bescherming")loSheet.cells(1,4).Value Iif(snLangue 1,"Photo","Picture")loSheet.cells(1,5).Value Iif(snLangue 1,"Prix","Prijs")

If snCont = 1 && si on ne demande pas les infos container, on ne crée pas les colonnes    loSheet.cells(1,6).Value Iif(snLangue 1,"prix container HC","Cont. prijs HC")    loSheet.cells(1,7).Value Iif(snLangue 1,"prix container 20P","Cont. prijs 20P")    loSheet.cells(1,8).Value Iif(snLangue 1,"prix container 40P","Cont. prijs 40P")
    loSheet.cells(1,9).Value = "Qty HC"
    loSheet.cells(1,10).Value = "Qty 20p"
    loSheet.cells(1,11).Value = "Qty 40p"
    loSheet.cells(1,12).Value = "usine"
Endif

*!*        select c_result
*!*        go top
*!*        do while !EOF()
*!*            loSheet.cells(lnLine,1).value = c_result.ref
*!*            loSheet.cells(lnLine,2).value = c_result.pcb
*!*            loSheet.cells(lnLine,3).value = c_result.nom
*!*            *if file(gcDataDir + "PICTURES\ART" + alltrim(c_result.ref)+".JPEG")
*!*            *    loSheet.range['D3','D3'].select
*!*            *    loSheet.pictures.insert(gcDataDir + "PICTURES\ART" + alltrim(c_result.ref)+".JPG")
*!*            *endif
*!*            *loSheet.cells(lnLine,4).value = "photo"
*!*            loSheet.cells(lnLine,5).value = c_result.prix
*!*
*!*            if snCont = 1 && si on ne demande pas les infos container, on ne rempli pas les colonnes*!*                loSheet.cells(lnLine,6).value iif(snCont 1,c_result.prix_conthc,"")*!*                loSheet.cells(lnLine,7).value iif(snCont 1,c_result.prix_cont20,"")*!*                loSheet.cells(lnLine,8).value iif(snCont 1,c_result.prix_cont40,"")*!*                loSheet.cells(lnLine,9).value iif(snCont 1,c_result.qty_contHC,"")*!*                loSheet.cells(lnLine,10).value iif(snCont 1,c_result.qty_cont20,"")*!*                loSheet.cells(lnLine,11).value iif(snCont 1,c_result.qty_cont40,"")
*!*                loSheet.cells(lnLine,12).value = c_result.c_usine
*!*            endif
*!*            lnLine = lnLine + 1
*!*            skip
*!*            loop
*!*        enddo
loExcel.Visible = .T.

** Quadrillage du rapport:

** Récupération de la dernière case en bas à droite utilisée
*!*        if snCount = 1
*!*            lcMonCell = "L"+str(lnLine)
*!*            messagebox(lcMonCell)
*!*        else
*!*            lcMonCell = "E"+str(lnLine)
*!*            messagebox(lcMonCell)
*!*        endif

loSheet.Range("A1:D20").Select
With loExcel
    .Selection.BorderS(xlDiagonalDown).LineStyle = xlNone
    .Selection.BorderS(xlDiagonalUp).LineStyle = xlNone
    With .Selection.BorderS(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    Endwith
    With .Selection.BorderS(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    Endwith
    With .Selection.BorderS(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    Endwith
    With .Selection.BorderS(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    Endwith
    With .Selection.BorderS(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    Endwith
    With .Selection.BorderS(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    Endwith
Endwith

Release loSheet, loBook, loExcel

Mike Gagnon
3
gregorian Messages postés 57 Date d'inscription lundi 18 avril 2005 Statut Membre Dernière intervention 24 mars 2011 1
14 mai 2007 à 10:41
Merci beaucoup.

Ca fonctionne à merveille.

Peux tu me dire ou je pourrais trouver une doc sur les fonctions excell pour savoir au moins dans qu'elle direction aller lorsque je dois faire ce genre de chose.

J'ai rechercher sur le net mais sans succès.

Merci
0
Mike Gagnon Messages postés 381 Date d'inscription vendredi 15 octobre 2004 Statut Membre Dernière intervention 24 octobre 2013 2
14 mai 2007 à 12:10
A part les #DEFINE, la meilleur facon de faire est lorsque tu veux faire quelque chose avec Excel, enregistre une Macro, fais ce que tu as a faire, ferme ta Macro, edite la macro, et tout le code ci trouve (En VB, mais tres proche de VFP). Tools-Macro-Record a new Macro.

Mike Gagnon
0
michelatoutfox Messages postés 828 Date d'inscription mardi 5 octobre 2004 Statut Membre Dernière intervention 7 mai 2013 1
14 mai 2007 à 13:18
Tu peux aussi utiliser l'explorateur d'objet de VFP, pour récupérer toutes les constantes de VB (celles que Mike redéfinit en constantes de précompilation), et les propriétés et méthodes utilisables en automation sur Excel
0
Rejoignez-nous