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

VBA Outlook Discussion :

Transfert data vers excel


Sujet :

VBA Outlook

  1. #1
    Membre à l'essai
    Inscrit en
    Février 2008
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 32
    Points : 17
    Points
    17
    Par défaut Transfert data vers excel
    Bonjour,
    Je souhaite envoyer vers un fichier excel des infos reçues par mail.

    Je dois vérifier si le fichier excel n'est pas déjà ouvert, et l'ouvrir si besoin ( cela à l'air de fonctionner )

    J'ai un soucis car je n'arrive pas remplir mes cellules dans mon fichier excel de réception des infos
    Je galère pour me faire comprendre alors je mets un bout de macro en espérant que cela vous aide.


    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
    Private Sub CommandButton2_Click()
     
    Dim myItems As Outlook.Items
    Dim myItem As Object
    Dim bus As String
    Dim fichier As String
    Dim fichier1 As String
    Dim a As String
    Dim appxl As Excel.Application    
    Dim wb As Excel.Workbook    
     
     
    fichier1 = "commande.xlsx"
    Call Fichier1XlsOuvert(fichier1)
     
    Set wb = appxl.Workbooks(fichier1)
     
     
    wb.Sheets("feuil1").Range("A65536").End(xlUp).Offset(1, 0) = UserForm1.Label4.Caption
     
    End sub
     
     
    Function Fichier1XlsOuvert(NomFichier1Xls As String) As Boolean
     
    On Error GoTo erreur
    Dim xla As Object
    Dim wb As Excel.Workbook    'Classeur Excel
    Dim fichier As String
     
    fichier = "C:\commande.xlsx"
    Fichier1XlsOuvert = False
     
    Set xla = GetObject(, "Excel.application")
     
     If xla.Workbooks(NomFichier1Xls).Name = NomFichier1Xls Then Fichier1XlsOuvert = True
     
     Exit Function
     
    erreur:
     
     Set xla = CreateObject("Excel.Application")
      xla.Visible = True
     Set wb = xla.Workbooks.Open(fichier)
    End Function

  2. #2
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    Salut,

    Si tu as une fonction, il faudrait qu'elle te renvoi un objet Excel.application ou Workbooks.

    Essaye comme cela
    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
    Private Sub CommandButton2_Click()
     
    Dim myItems As Outlook.Items
    Dim myItem As Object
    Dim bus As String
    Dim fichier As String
    Dim fichier1 As String
    Dim a As String
    Dim appxl As Excel.Application    
    Dim wb As Excel.Workbook    
     
     
    fichier1 = "commande.xlsx"
     
    Set wb = Fichier1XlsOuvert(fichier1)
     
     
    wb.Sheets("feuil1").Range("A65536").End(xlUp).Offset(1, 0).value = UserForm1.Label4.Caption
     
    End sub
     
     
    Function Fichier1XlsOuvert(NomFichier1Xls As String) As object
     
    On Error GoTo erreur
    Dim xla As Object
    Dim wb As Excel.Workbook    'Classeur Excel
    Dim fichier As String
     
    Fichier1XlsOuvert = False
     
    Set xla = GetObject(, "Excel.application")
    
    If xla.Workbooks(NomFichier1Xls).Name = NomFichier1Xls Then NomFichier1Xls = xla.Workbooks(NomFichier1Xls)
     
     Exit Function
     
    erreur:
     
    Set xla = CreateObject("Excel.Application")
    xla.Visible = True
    Set NomFichier1Xls= xla.Workbooks.Open(fichier)
    End Function
    sinon tu peux faire toutes tes opération excel dans ta focntion (ou Sub)

  3. #3
    Membre à l'essai
    Inscrit en
    Février 2008
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 32
    Points : 17
    Points
    17
    Par défaut
    Salut Oliv-

    Avec ta solution, j'ai une erreur "objet requis" sur la ligne 42

    Set NomFichier1Xls= xla.Workbooks.Open(fichier)

  4. #4
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    cette fois je l'ai testé.
    attention il te faut le chemin complet du fichier ou alors il faut que le dirpath soit positionnée sur le dossier en question.

    GetObject(, "Excel.application") a aussi ses limites si tu as 2 instances de Excel ouvertes !

    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
    Private Sub CommandButton2_Click()
     
    'Dim myItems As Outlook.Items
    'Dim myItem As Object
    'Dim bus As String
    Dim fichier As String
    Dim fichier1 As String
    Dim a As String
    Dim appxl As Excel.Application
    Dim wb As Excel.Workbook
     
     
    fichier1 = "C:\temp\Liste (v001).xls"
     
    Set wb = Fichier1XlsOuvert(fichier1)
     
     
    wb.Sheets("feuil1").Range("A65536").End(xlUp).Offset(1, 0).Value = "toto" 'UserForm1.Label4.Caption
     
    End Sub
     
     
    Function Fichier1XlsOuvert(NomFichier1Xls As String) As Object
     
    On Error GoTo erreur
    Dim xla As Object
    Dim wb As Excel.Workbook    'Classeur Excel
    Dim fichier As String
     
    Set xla = GetObject(, "Excel.application")
    NomFichier = StrReverse(Split(StrReverse(NomFichier1Xls), "\")(0))
     
    If xla.Workbooks(NomFichier).Name = NomFichier Then Fichier1XlsOuvert = xla.Workbooks(NomFichier)
     
    Exit Function
     
    erreur:
     
    Set xla = Excel.Application
    xla.Visible = True
    Set Fichier1XlsOuvert = xla.Workbooks.Open(NomFichier1Xls)
    End Function

  5. #5
    Membre à l'essai
    Inscrit en
    Février 2008
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 32
    Points : 17
    Points
    17
    Par défaut
    Merci pour ton aide....
    J'ai trouvé une solution un peu différente qui marche......
    je poste mon code

    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
    Private Sub CommandButton2_Click()
     
    Dim myItems As Outlook.Items
    Dim myItem As Object
    Dim wb As Excel.Workbook
    Dim appxl As Excel.Application
    Dim xla As Object
     
     
    Dim a As String
    Dim b As String
    Dim c As String
     
    Dim bus As String
    Dim fichier As String
    Dim fichier1 As String
     
     
    Dim i As Long
    Dim j As Long
     
    b = Now
    b = Format(b, "YYYY")
     
     
    fichier1 = "commande.xlsx"
    fichier = "C:\commande.xlsx"
     
    Set appxl = Excel.Application '
    j = appxl.Workbooks.Count ' compte nmbre fichier excel ouverts
    If j = 0 Then GoTo 2 ' si 0 alors ouvre le fichier commande goto 2:
     For i = 1 To j
     c = appxl.Workbooks(i).Name
     
     If c = fichier1 Then
     GoTo 1
     End If
     Next i
     
    2:
    Set wb = appxl.Workbooks.Open(fichier)
    appxl.Visible = True
     
    1:
     
    Set wb = appxl.ActiveWorkbook
     
     
    wb.Sheets(b).Range("A65536").End(xlUp).Offset(1, 0) = UserForm1.Label4.Caption
    wb.Sheets(b).Range("A65536").End(xlUp).Offset(0, 9) = UserForm1.Label2.Caption
     
    If Not InStr(1, UserForm1.Label6.Caption, "le bus N°", 1) = 0 Then
            bus = Mid(UserForm1.Label6.Caption, 11, Len(UserForm1.Label6.Caption))
            wb.Sheets(b).Range("A65536").End(xlUp).Offset(0, 11) = bus
            wb.Sheets(b).Range("A65536").End(xlUp).Offset(0, 8) = "Bus"
    End If
     
     If Not InStr(1, UserForm1.Label6.Caption, "N° d'Ordre", 1) = 0 Then
     
        a = UserForm1.MultiPage1.Pages(0).Label8.Caption
            wb.Sheets(b).Range("A65536").End(xlUp).Offset(0, 11) = a
     
        a = UserForm1.MultiPage1.Pages(0).Label25.Caption
            wb.Sheets(b).Range("A65536").End(xlUp).Offset(0, 8) = a
        a = UserForm1.MultiPage1.Pages(0).Label11.Caption
            wb.Sheets(b).Range("A65536").End(xlUp).Offset(0, 14) = a
     
    End If
     
    MsgBox "Infos ajoutées dans tableau"
    Unload UserForm1

  6. #6
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    C'est effectivement une autre façon de faire. mais ton
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Set appxl = Excel.Application
    va systématiquement t'ouvrir un NOUVEAU excel.

    Par contre ma remarque sur le nombre de sessions excel reste valable. !

    Tu pourrais faire le contraire, ouvrir toujours le fichier excel avec appxl.Workbooks.Open(fichier) et si le fichier est en lecture seule c'est qu'il est déjà ouvert et dans ce cas passer par un getobject

  7. #7
    Membre à l'essai
    Inscrit en
    Février 2008
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 32
    Points : 17
    Points
    17
    Par défaut
    Oui tu as le code pour cela

    If fichier is open ??

  8. #8
    Expert éminent
    Avatar de Oliv-
    Homme Profil pro
    solution provider
    Inscrit en
    Mars 2006
    Messages
    4 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : solution provider

    Informations forums :
    Inscription : Mars 2006
    Messages : 4 087
    Points : 7 168
    Points
    7 168
    Billets dans le blog
    20
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    if wb.ReadOnly then 
     
    Set xla = GetObject(, "Excel.application")
    NomFichier = StrReverse(Split(StrReverse(NomFichier1Xls), "\")(0))
     
    If xla.Workbooks(NomFichier).Name = NomFichier Then wb= xla.Workbooks(NomFichier)
    end if

Discussions similaires

  1. souci avec transfert as400 vers excell
    Par moudcoul dans le forum Autres systèmes
    Réponses: 0
    Dernier message: 15/07/2009, 16h59
  2. Transfert formulaire vers excel
    Par Tequila_PF dans le forum VBA Access
    Réponses: 13
    Dernier message: 23/06/2008, 23h51
  3. VBA Excel : Transfert listbox vers Excel
    Par Secco dans le forum Macros et VBA Excel
    Réponses: 17
    Dernier message: 27/04/2008, 23h33
  4. Esport de data vers excel.
    Par splach dans le forum ASP.NET
    Réponses: 1
    Dernier message: 20/03/2008, 18h04
  5. Transfert requette ver excel en VB
    Par mbokmahop dans le forum VBA Access
    Réponses: 8
    Dernier message: 07/06/2007, 14h51

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