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 Access Discussion :

Erreur définie par l'Application ou par l'Objet (N°1004)


Sujet :

VBA Access

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Février 2007
    Messages
    755
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 755
    Points : 208
    Points
    208
    Par défaut Erreur définie par l'Application ou par l'Objet (N°1004)
    Bonjour à tous,
    J'ai besoin d'exporter des données Access 2010 vers Excel 2010, sur ma base Access j'ai défini un bouton permettant sur l'évènement "sur click" de faire un export d'une table vers un classeur 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
    Public Sub ProcExportExcel(onglet)
    Dim xlApp As Object 'Application Excel
    Dim oWkb As Object 'Classeur
    Dim oWSht As Object  'Feuille de Calcul
    Dim Cell As Range
    Dim ligne As Long
    Dim col1 As Integer
    Dim col2 As Integer
    Dim col3 As Integer
    Dim col4 As Integer
    Dim col5 As Integer
    Dim lignetrouvee As Range
    Dim bd As DAO.Database
    Set bd = CurrentDb
    Dim RecSet As DAO.Recordset
    Dim cSQL As String
    Dim NumInsert As String
    Dim NumInsertCell As Range
    Dim Num_Arch As String
    Dim V_ADRESS_DOSS As String
    Dim DM As String
    Dim Empl As String
    Dim ind_onglet As Variant
    Dim Choix_ligne As String
    Dim Num_ligne As Integer
    ' Créer un objet Excel' (ce qui équivaut à démarrer Excel à distance)
    Set xlApp = CreateObject("Excel.Application")
    cSQL = "SELECT N°Insertion,NUM_Archives,Adress_Doss, TAB_DM.DM,TAB_DM.EMPLACEMENT " & _
    "FROM TAB_INSERTIONS INNER JOIN TAB_DM ON TAB_INSERTIONS.DM = TAB_DM.DM " & _
    "WHERE Tab_DM.DM ='" & Forms!F_Ges_DM!Liste9 & "'" & "" & _
    "ORDER BY Tab_Insertions.Date_Trait DESC,Tab_Insertions.N°Insertion;"
    
    Set RecSet = bd.OpenRecordset(cSQL)
    With xlApp
    Set oWkb = xlApp.Workbooks.Open(DLookup("[Chemin_Fichier_Export]", "TAB_PARAMETRE") & DLookup("[Nom_Fichier_Export]", "TAB_PARAMETRE"))
    For Each oWSht In oWkb.Sheets
              If oWSht.Name = onglet Then
                
                ind_onglet = oWSht.index
                Exit For
              End If
                Next
    On Error GoTo Ges_Err
    ligne = 2
    col1 = 1
    col2 = 2
    col3 = 3
    col4 = 4
    col5 = 5
    Num_ligne = 2
    Choix_ligne = "A" & Num_ligne & ":E" & Num_ligne & ""
    RecSet.MoveFirst
    Set lignetrouvee = oWSht.Range("A2:A2000").Find(Not Null, lookat:=xlPart)
    Do While Not RecSet.EOF Or lignetrouvee Is Nothing
    NumInsert = RecSet.Fields("N°Insertion")
    Num_Arch = RecSet.Fields("NUM_Archives")
              If Not IsNull(RecSet.Fields("Adress_Doss")) Then
                V_ADRESS_DOSS = RecSet.Fields("Adress_Doss")
              End If
              DM = RecSet.Fields("DM")
              Empl = RecSet.Fields("Emplacement")
                 Cells(ligne, col1).Select     
                 Cells(ligne, col1).Value = NumInsert    
                 Cells(ligne, col2).Select
                 Cells(ligne, col2).Value = Num_Arch
                 Cells(ligne, col3).Select
                 Cells(ligne, col3).Value = V_ADRESS_DOSS
                 Cells(ligne, col4).Select
                 Cells(ligne, col4).Value = DM
                 Cells(ligne, col5).Select
                 Cells(ligne, col5).Value = Empl
     ligne = ligne + 1
              Num_ligne = Num_ligne + 1
              Choix_ligne = "A" & Num_ligne & ":E" & Num_ligne & ""
              If Not RecSet.EOF Then
                RecSet.MoveNext
              End If
    Loop
    ' Sauvegarder et fermer le classeur
            oWkb.Save
            oWkb.Close
    ' Quitter Excel
            .Quit
    ' Libérer les variables objet
        Set oWSht = Nothing 'Feuille de Calcul
        Set oWkb = Nothing   'Classeur
        Set xlApp = Nothing  'Excell
    FinGes_err:
      Exit Sub
    Ges_Err:
        If err = 9 Then MsgBox "Attention ! Onglet " & onglet & " n'existe pas dans le fichier Export Prière d'en informer les Référents  ", _
         vbOKOnly + vbCritical, _
         "Export Excel "
    MsgBox err.Description & " " & err.Number
           ' Sauvegarder et fermer le classeur
            oWkb.Save
            oWkb.Close
           ' Libérer les variables objet
           ' Quitter Excel
            .Quit
        End With ' Libérer les variables objet
          Set oWSht = Nothing 'Feuille de Calcul
          Set oWkb = Nothing   'Classeur
          Set xlApp = Nothing  'Excell
          Resume FinGes_err
    End Sub
    Ce code fonctionne bien sur le 1er clic mais quand je veux refaire un export (2ème clic) j'ai l'erreur "Erreur définie par l'Application ou par l'Objet " (n°1004)
    L'erreur se situe sur la ligne 62 en rouge
    Si quelqu'un a une idée
    Merci

  2. #2
    Expert confirmé Avatar de nico84
    Homme Profil pro
    Consultant/développeur ERP
    Inscrit en
    Mai 2008
    Messages
    3 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Consultant/développeur ERP
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 3 087
    Points : 5 203
    Points
    5 203
    Par défaut
    Bonjour,

    Personnellement je précise toujours la feuille quand j'utilise la fonction cell, par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    with activesheet
     .cells(...
    end with
    Utilisez Planet, gestion d'entreprise gratuite pour TPE / PME

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Février 2007
    Messages
    755
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 755
    Points : 208
    Points
    208
    Par défaut
    Citation Envoyé par nico84 Voir le message
    Bonjour,

    Personnellement je précise toujours la feuille quand j'utilise la fonction cell, par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    with activesheet
     .cells(...
    end with
    **************************************
    Bjr et merci pour ta réponse j'ai essayé et non ca change rien

    Merci

  4. #4
    Expert éminent sénior
    Avatar de tee_grandbois
    Homme Profil pro
    retraité
    Inscrit en
    Novembre 2004
    Messages
    8 637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : retraité

    Informations forums :
    Inscription : Novembre 2004
    Messages : 8 637
    Points : 14 611
    Points
    14 611
    Par défaut
    bonjour,
    Bjr et merci pour ta réponse j'ai essayé et non ca change rien
    on aurait bien aimé malgré tout voir le nouveau code ...
    étant donné que la feuille active est définie ici : For Each oWSht In oWkb.Sheets j'opterai plutôt pour:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    oWSht.Cells(ligne, col1).Select 
    oWSht.Cells(ligne, col1).Value = NumInsert
    Quand on est derrière l'écran on n'a aucun clavier sous les mains ...
    ah non ? donc devant l'écran c'est la connectique ?

  5. #5
    Membre actif
    Profil pro
    Inscrit en
    Février 2007
    Messages
    755
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 755
    Points : 208
    Points
    208
    Par défaut
    Citation Envoyé par tee_grandbois Voir le message
    bonjour,

    on aurait bien aimé malgré tout voir le nouveau code ...
    étant donné que la feuille active est définie ici : For Each oWSht In oWkb.Sheets j'opterai plutôt pour:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    oWSht.Cells(ligne, col1).Select 
    oWSht.Cells(ligne, col1).Value = NumInsert
    *****************************************************
    Merci ca marche !
    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
     
    Public Sub ProcExportExcel(onglet)
    Dim xlApp As Excel.Application 'Appli Excel
    Dim oWkb As Excel.Workbook 'Classeur
    Dim oWSht As Excel.Worksheet  'Feuille de Calcul
    Dim Cell As Range
    Dim ligne As Long
    Dim col1 As Integer
    Dim col2 As Integer
    Dim col3 As Integer
    Dim col4 As Integer
    Dim col5 As Integer
    Dim lignetrouvee As Range
    Dim bd As DAO.Database
    Set bd = CurrentDb
    Dim RecSet As DAO.Recordset
    Dim cSQL As String
    Dim NumInsert As String
    Dim NumInsertCell As Range
    Dim Num_Arch As String
    Dim V_ADRESS_DOSS As String
    Dim DM As String
    Dim Empl As String
    Dim ind_onglet As Variant
    Dim Choix_ligne As String
    Dim Num_ligne As Integer
    ' Créer un objet Excel' (ce qui équivaut à démarrer Excel à distance)
    Set xlApp = CreateObject("Excel.Application")
    cSQL = "SELECT N°Insertion,NUM_Archives,Adress_Doss, TAB_DM.DM,TAB_DM.EMPLACEMENT " & _
    "FROM TAB_INSERTIONS INNER JOIN TAB_DM ON TAB_INSERTIONS.DM = TAB_DM.DM " & _
    "WHERE Tab_DM.DM ='" & Forms!F_Ges_DM!Liste9 & "'" & "" & _
    "ORDER BY Tab_Insertions.Date_Trait DESC,Tab_Insertions.N°Insertion;"
     
    Set RecSet = bd.OpenRecordset(cSQL)
    With xlApp
    Set oWkb = xlApp.Workbooks.Open(DLookup("[Chemin_Fichier_Export]", "TAB_PARAMETRE") & DLookup("[Nom_Fichier_Export]", "TAB_PARAMETRE"))
    For Each oWSht In oWkb.Sheets
              If oWSht.Name = onglet Then
     
                ind_onglet = oWSht.index
                Exit For
              End If
                Next
    On Error GoTo Ges_Err
    ligne = 2
    col1 = 1
    col2 = 2
    col3 = 3
    col4 = 4
    col5 = 5
    Num_ligne = 2
    Choix_ligne = "A" & Num_ligne & ":E" & Num_ligne & ""
    RecSet.MoveFirst
    Set lignetrouvee = oWSht.Range("A2:A2000").Find(Not Empty, lookat:=xlPart)
    Do While Not RecSet.EOF And lignetrouvee Is Nothing
     NumInsert = RecSet.Fields("N°Insertion")
    Num_Arch = RecSet.Fields("NUM_Archives")
              If Not IsNull(RecSet.Fields("Adress_Doss")) Then
                V_ADRESS_DOSS = RecSet.Fields("Adress_Doss")
              End If
              DM = RecSet.Fields("DM")
              Empl = RecSet.Fields("Emplacement")
     With ActiveSheet
        oWSht.Cells(ligne, col1).Select
                 oWSht.Cells(ligne, col1).Value = NumInsert
                 oWSht.Cells(ligne, col2).Select
                 oWSht.Cells(ligne, col2).Value = Num_Arch
                 oWSht.Cells(ligne, col3).Select
                 oWSht.Cells(ligne, col3).Value = V_ADRESS_DOSS
                 oWSht.Cells(ligne, col4).Select
                 oWSht.Cells(ligne, col4).Value = DM
                 oWSht.Cells(ligne, col5).Select
                 oWSht.Cells(ligne, col5).Value = Empl
     End With
     ligne = ligne + 1
              Num_ligne = Num_ligne + 1
              Choix_ligne = "A" & Num_ligne & ":E" & Num_ligne & ""
              If Not RecSet.EOF Then
                RecSet.MoveNext
              End If
    Loop
    MsgBox "Export réussi... ", _
         vbOKOnly, _
         "Export Excel "
    ' Sauvegarder et fermer le classeur
            oWkb.Save
            oWkb.Close
    ' Quitter Excel
            .Quit
    ' Libérer les variables objet
        Set oWSht = Nothing 'Feuille de Calcul
        Set oWkb = Nothing   'Classeur
        Set xlApp = Nothing  'Excell
    FinGes_err:
      Exit Sub
    Ges_Err:
        If err = 9 Then MsgBox "Attention ! Onglet " & onglet & " n'existe pas dans le fichier Export Prière d'en informer les Référents  ", _
         vbOKOnly + vbCritical, _
         "Export Excel "
        MsgBox err.Description & " " & err.Number
           ' Sauvegarder et fermer le classeur
            oWkb.Save
            oWkb.Close
           ' Libérer les variables objet
           ' Quitter Excel
            .Quit
        End With ' Libérer les variables objet
          Set oWSht = Nothing 'Feuille de Calcul
          Set oWkb = Nothing   'Classeur
          Set xlApp = Nothing  'Excell
          Resume FinGes_err
     
    End Sub
    A +

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

Discussions similaires

  1. [XL-2016] Erreur d'excécution 1004 - Erreur définie par l'objet ou l'application
    Par Eddie69003 dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 14/12/2016, 11h26
  2. Réponses: 2
    Dernier message: 20/10/2009, 16h59
  3. Réponses: 2
    Dernier message: 28/10/2008, 09h30
  4. [VBA-E]erreur définie par l'application ou par l'objet
    Par vivelesgnous dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 15/02/2006, 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