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

Macros et VBA Excel Discussion :

[VB]Ecrire dans un fichier excell


Sujet :

Macros et VBA Excel

  1. #1
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut [VB]Ecrire dans un fichier excell
    Salut,
    Tout d'abord, merci à tout ceux qui m'ont aidé hier... Je me permet de vous soliciter à nouveau aujourd'hui car j'ai une nouvelle question... J'aimerais savoir si il est possible d'écrire dans un fichier xls depuis le vb. Ce que j'aimerais faire c'est écrire 1 ligne dans un tableau excel à la suite de celle qui s'y trouve déjà... Les lignes sont des infos reprises sur différents pc (nom de machine, login,...)
    Est-ce réalisable? Comment faire?
    D'avance merci.
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 46
    Points : 55
    Points
    55
    Par défaut
    Oui c'est réalisable ça demande un peu de boulot !

    Piste de recherche CreateObject("Excel.Application")

    à partir de ça à toi de trouver toute les infos !

  3. #3
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Merci, je vais jeter un oeil... Et essayer de comprendre...
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  4. #4
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Je suis allé voir mais pour l'instant, c'est pas très concluant. J'ai trouvé ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    'Déclaration des variables
    Dim appExcel As Excel.Application 'Application Excel
    Dim wbExcel As Excel.Workbook 'Classeur Excel
    Dim wsExcel As Excel.Worksheet 'Feuille Excel
     
    'Ouverture de l'application
    Set appExcel = CreateObject("Excel.Application")
    'Ouverture d'un fichier Excel
    Set wbExcel = appExcel.Workbooks.Open("C:\Documents and Settings\Desktop\ess.xls")
    'wsExcel correspond à la première feuille du fichier
    Set wsExcel = wbExcel.Worksheets(1)
     
    End Sub
    Qui me ressort des messages d'erreur de type de variable indéfini lors des déclarations... A quoi cela est-il lié??? Je débute en vb...
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 46
    Points : 55
    Points
    55
    Par défaut
    Il faut que tu rajoute dans les références de ton projet la référence Microsoft Excel X.X Object Library !

    les X.X correspondant au numéro de version Excel de ton PC !

    Ensuite une fois que tu as fini, si tu dois diffuser ton appli et que la version d'excel risque de ne pas être la même, tu supprime ta référence à Excel et tu redéclare tes variables en As Object au lieu de As Excel.Application, ...

  6. #6
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    En cherchant encore, je suis arrivé au code suivant qui me conviens somme toute bien à 1 détail près :
    Je ne sais toujours pas comment trouver la dernière ligne remplie.
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  7. #7
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Ma source :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Dim objexcel As Object
    Dim i As Long
    Set objexcel = CreateObject("Excel.Application")
    objexcel.workbooks.Add
    For i = 1 To 5
    objexcel.range("d" & i).formula = "Je teste"
    Next i
    objexcel.workbooks(1).saveas "C:\Documents and Settings\carlilo\Desktop\ess.xls"
    objexcel.Quit
    Set objexcel = Nothing
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  8. #8
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 038
    Points
    20 038
    Par défaut
    Citation Envoyé par Empty_body
    En cherchant encore, je suis arrivé au code suivant qui me conviens somme toute bien à 1 détail près :
    Je ne sais toujours pas comment trouver la dernière ligne remplie.
    si tu fais une petite recherche dans le forum tu devrai trouver plein de solution, en voila une qui fonctionne si toutes les lignes sont contigu..(pas de ligne vide)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     
    dernierLigne= objexcel.sheet(1).Range("A1").CurrentRegion.Rows.Count


  9. #9
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Merci... Je vais continuer à chercher...
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  10. #10
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Je viens d'essayer ta ligne de code et j'ai l'erreur suivante : "Object does not support this property or method"... Comment ça se fait???? Je suis sous xp et office xp...
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 46
    Points : 55
    Points
    55
    Par défaut
    enlève ".sheet(1)"

  12. #12
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Effectivement, ça fonctionne tout de suite mieux comme ça... J'arrive bien à la dernière ligne... Le problème qui se pose maintenant est que je n'ai que la dernière ligne dans le fichier, les précédentes sont effacées... Comment faire pour les garder???
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  13. #13
    Membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 46
    Points : 55
    Points
    55
    Par défaut
    j'ai fais le teste j'ouvre le fichier je teste les lignes j'écris sur la dernière et je sauvegarde aucun souci !?

  14. #14
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Si le fichier n'est pas vide, le fait de lancer le prog, il n'y aura aucune erreur, le problème est que si tu vas voir dans le .xls, seule une ligne apparaît, la dernière... Les autres ont toute été virées...
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  15. #15
    Nouveau membre du Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2006
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Distribution

    Informations forums :
    Inscription : Février 2006
    Messages : 26
    Points : 32
    Points
    32
    Par défaut Export d'une MSFlex Grid sous Excel .....
    Je post ce que j'ai fait il y a mantenant quelques années... export de list (MSFlex Grid) sous Excel ......

    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
    Sub ExportGenerique(Z As MSFlexGrid)
    Dim intI As Integer, IntC As Integer, IntCEx As Integer, IntIEx As Integer
    Dim NbCol As Integer, NbRow As Integer
    Dim TempString As String
    On Error GoTo ErreurEx
    Call InitEXCEL(UneErreur, TexteErreur)
    If UneErreur Then
        MsgBox "Excel n'a pas pu Etre Lancé" & vbCrLf & "Erreur : " & TexteErreur
    Else
        With Z
        intI = 0
        IntIEx = 0
        NbCol = Z.Cols
        NbRow = Z.Rows
        intI = 0
        While intI < NbRow
            .Row = intI
            IntC = 0
            IntCEx = 1
            While IntC < NbCol
                .Col = IntC
     
                ExcelObj.Cells(IntIEx + 2, IntCEx + 1).Value = .Text
                If IntIEx = 0 Then
                    ExcelObj.Cells(IntIEx + 2, IntCEx + 1).Font.Size = 14
                End If
                IntC = IntC + 1
     
                IntCEx = IntCEx + 1
            Wend
            intI = intI + 1
            IntIEx = IntIEx + 1
        Wend
        ExcelObj.Range(IntituleCellule(2, 2) & ":" & IntituleCellule(IntCEx, IntIEx + 1)).Select
        Call BordureTDT
        ExcelObj.Range(IntituleCellule(1, 1) & ":" & IntituleCellule(2, 1)).Select
        ExcelObj.Selection.Font.Size = 15
        ExcelObj.Cells.Select
        ExcelObj.Selection.HorizontalAlignment = xlCenter
        ExcelObj.Selection.VerticalAlignment = xlCenter
        ExcelObj.Selection.ColumnWidth = 2.57
        ExcelObj.Selection.RowHeight = 15
        ExcelObj.Selection.RowHeight = 30
        ExcelObj.Cells.EntireColumn.AutoFit
        Set ExcelObj = Nothing
        End With
    End If
    Exit Sub
    ErreurEx:
    MsgBox "Erreur : " & Err.Description
    End Sub
     
    Sub InitEXCEL(Erreur As Boolean, Texte As String)
    Err.Clear
    On Error Resume Next
     
    Dim xlApp As Excel.Application
     
    Set xlApp = CreateObject("Excel.Application")
     
    Set ExcelObj = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
        Erreur = True
        Texte = Err.Description
        Err.Clear
    Else
        Erreur = False
        With ExcelObj
        .Visible = True
        .Workbooks.Add
        .Sheets.Add
        .Cells.Select
        .Cells.Clear
        End With
    End If
    End Sub
     
    Function IntituleCellule(LaColonne As Integer, LaLigne As Integer) As String
    Dim x As String
    x = ""
    If LaColonne <= 26 Then
        x = Chr(LaColonne + 64)
    ElseIf LaColonne <= 52 Then
        x = "A"
        x = x & Chr(LaColonne - 26 + 64)
    Else
        Exit Function
    End If
    x = x & LaLigne
    IntituleCellule = x
    End Function
     
    Sub BordureTDT()
    With ExcelObj
        With .Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With .Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With .Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With .Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlMedium
            .ColorIndex = xlAutomatic
        End With
        With .Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With .Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        .ActiveWindow.DisplayGridlines = False
    End With
    End Sub
    Voila c'est très brut ..... on fait surement mieux aujourd'hui ..... il doit aussi falloir rajouter les références qui vont bien pour Excel ..... (Microsoft Excel xx.x Object Library)...... J'espère que tu trouveras ton bonheur

  16. #16
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Pour l'instant, ça génère pas mal d'erreurs mais je vais essayer de comprendre ta source... Merci.
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  17. #17
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Si les erreurs sont liées à Microsoft Excel xx.x Object Library, je ne peux rien y faire car mon applic est destinée à être déployée sur un parc info, hors de question donc de paramètrer quoique ce soit... Existe-t-il une solution???
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

  18. #18
    Nouveau membre du Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2006
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Distribution

    Informations forums :
    Inscription : Février 2006
    Messages : 26
    Points : 32
    Points
    32
    Par défaut a l'époque
    cette fonction faisait partie d'une application deployée sur de nombreux postes qui n'avait pas forcement la meme version d'office ..... Ca marchait bien à l'époque mais il n'y avait pas XP ....

  19. #19
    Membre actif Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Points : 239
    Points
    239
    Par défaut
    Je viens de me rendre compte d'un truc, cette technique ne peut convenir à ce que je cherche à obtenir car en cas de tentative d'accès simultané, ça plante... Sorry d'avoir ennuyé mon peuple...
    Pourquoi vouloir ré-inventer la roue...
    ...Surtout si c'est pour la faire carrée...

Discussions similaires

  1. Lire/Ecrire dans un fichier excel
    Par toutounesan dans le forum C++
    Réponses: 5
    Dernier message: 14/09/2007, 22h07
  2. ecrire dans un fichier excel
    Par micka180 dans le forum VBScript
    Réponses: 3
    Dernier message: 01/08/2007, 15h23
  3. [C#] Comment écrire dans un fichier Excel ?
    Par chorokari dans le forum C#
    Réponses: 8
    Dernier message: 09/02/2007, 09h17
  4. comment ecrire dans un fichier excel
    Par dietrich dans le forum Général Dotnet
    Réponses: 1
    Dernier message: 15/01/2007, 12h46
  5. [C#] Comment écrire dans un fichier Excel ?
    Par FoxDeltaSierra dans le forum ASP.NET
    Réponses: 2
    Dernier message: 07/05/2006, 15h29

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