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

Access Discussion :

pb de sauvegarde de fichier excel


Sujet :

Access

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    74
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 74
    Points : 33
    Points
    33
    Par défaut pb de sauvegarde de fichier excel
    Bonjour,
    J ai un programme access qui fait appel a un prog Excel.
    Ca fonctionne tres bien, mais je voudrais passer la macro excel sous access afin d avoir juste un seul fichier ( d ailleur, au niveau des perfo, vous pense que ca va etre beaucoup moins bien ?)
    Le pb, c juste au niveau de la sauvegarde du fichier :
    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
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
     
    Private Sub Command4_Click()
    'Initialize the sheet
    If XL Is Nothing Then
        OpenXL
    End If
    XL.SheetsInNewWorkbook = 1
    XL.Workbooks.Add
    XL.Cells.Clear
    'Here below is the list of known tags in the XML document to import
    XL.Cells(1, 1).Value = "idnum"
    XL.Cells(1, 2).Value = "location"
    XL.Cells(1, 3).Value = "source"
    XL.Cells(1, 4).Value = "field"
    XL.Cells(1, 5).Value = "edit"
    XL.Cells(1, 6).Value = "rank"
    XL.Cells(1, 7).Value = "hw"
    XL.Cells(1, 8).Value = "vg"
    XL.Cells(1, 9).Value = "def"
    XL.Cells(1, 10).Value = "rg"
    XL.Cells(1, 11).Value = "ety"
    'open File
    Open Text1.Value For Input As #1
    'read File
    lineNumber = 1
    continuation = False
    Dim lineForContinuation As String
    Dim tagForContinuation As String
    Do Until EOF(1)
    Line Input #1, Data
    If Left(Data, 4) = "<en>" Then
    'Starting on the second line  because the first one is for fields names
        lineNumber = lineNumber + 1
        columnNumber = 1
        continuation = False
    'create a field if it does not exist
    Else
    If (Left(Data, 1) = "<" And continuation = False) Then
        endOfTag = InStr(Data, ">")
        startTag = Mid(Data, 2, endOfTag - 2)
        'the program goes back here if it adds a new field because it must explore the line then
    addFieldLoop:
        'compare the tag to the fields name (1st line of the excel sheet)
        'first we have to find how many fields exists (1st row) because some fields might have been added
        continu = True
        g = 1
        fieldsTotal = 1
        Do While continu
            If XL.Cells(1, g).Value = "" Then
            fieldsTotal = g - 1
            continu = False
            Else
            g = g + 1
            End If
        Loop
        'now we check if a corresponding field exists (the case "cod" is added because this case is special (split in 3 fields))
        compareOK = False
        For i = 1 To fieldsTotal
            If startTag = XL.Cells(1, i).Value Or startTag = "cod" Then
                compareOK = True
                columnNumber = i
            End If
            If startTag = "headword" Then
                compareOK = True
                columnNumber = 7
            End If
        Next i
        If compareOK Then
        'analyse the rest of data to find the ending tag (if not found then the next line will be the continuation) and to remove other existing tags
        endOfCurrentTag = 0
        currentLine = Right(Data, Len(Data) - endOfTag)
    endFinder:
        nextTagPosition = InStr(endOfCurrentTag + 1, currentLine, "<")
            If nextTagPosition = 0 Then
            'the end tag was not found and is in another line
            continuation = True
            tagForContinuation = startTag
            lineForContinuation = currentLine
            Else
            If (Mid(currentLine, nextTagPosition + 1, 1) = "/" And Mid(currentLine, nextTagPosition + 2, Len(startTag)) = startTag) Then
            'the end tag is the one found and we have check that the name is ths same than ths starting tag
            currentLine = Left(currentLine, nextTagPosition - 1)
            continuation = False
            lineForContinuation = ""
            tagForContinuation = ""
            'the record is added
                If startTag <> "cod" Then
                    If Left(currentLine, 1) <> "=" And Left(currentLine, 1) <> "-" Then
                        XL.Cells(lineNumber, columnNumber).Value = currentLine
                    Else
                        XL.Cells(lineNumber, columnNumber).Value = "'" & currentLine
                    End If
                Else
                'Here we analyse the code to slip it
                   ' we have to skip in case the line is empty or else an error occures
                    If currentLine = "" Or currentLine = "/" Then
                        GoTo jump
                    Else
                        If Left(currentLine, 1) = "/" Then
                        'just checking if the first character is a "/" and if so removing it
                            currentLine = Right(currentLine, Len(currentLine) - 1)
                        End If
     
                        If InStr(currentLine, "/") <> 0 Then
                            If Left(currentLine, InStr(currentLine, "/") - 1) = "B" _
                            Or Left(currentLine, InStr(currentLine, "/") - 1) = "D" _
                            Then
                                XL.Cells(lineNumber, 2).Value = Left(currentLine, InStr(currentLine, "/") - 1)
                                currentLine = Right(currentLine, Len(currentLine) - InStr(currentLine, "/"))
                                If currentLine = "" Then
                                ' skip if empty line
                                    GoTo jump
                                End If
                            End If
                        Else
                            If currentLine = "B" _
                            Or currentLine = "D" _
                            Then
                                XL.Cells(lineNumber, 2).Value = currentLine
                                'the currentline is obviously finished and there is no need to go further
                                GoTo jump
                            End If
                        End If
     
                        If InStr(currentLine, "/") <> 0 Then
                            If Left(currentLine, InStr(currentLine, "/") - 1) = "LONG" _
                            Or Left(currentLine, InStr(currentLine, "/") - 1) = "APA" _
                            Or Left(currentLine, InStr(currentLine, "/") - 1) = "THES" _
                            Or Left(currentLine, InStr(currentLine, "/") - 1) = "C" _
                            Or Left(currentLine, InStr(currentLine, "/") - 1) = "P" _
                            Or Left(currentLine, InStr(currentLine, "/") - 1) = "PC" _
                            Then
                                XL.Cells(lineNumber, 3).Value = Left(currentLine, InStr(currentLine, "/") - 1)
                                currentLine = Right(currentLine, Len(currentLine) - InStr(currentLine, "/"))
                                If currentLine = "" Then
                                ' skip if empty line
                                    GoTo jump
                                End If
                            End If
                            If Right(currentLine, 1) <> "/" Then
                                XL.Cells(lineNumber, 4).Value = currentLine
                            Else
                                XL.Cells(lineNumber, 4).Value = Left(currentLine, Len(currentLine) - 1)
                            End If
                        Else
                            If currentLine = "LONG" _
                            Or currentLine = "APA" _
                            Or currentLine = "THES" _
                            Or currentLine = "C" _
                            Or currentLine = "P" _
                            Or currentLine = "PC" _
                            Then
                                XL.Cells(lineNumber, 3).Value = currentLine
                                'the currentline is obviously finished and there is no need to go further
                                GoTo jump
                            End If
                            If Right(currentLine, 1) <> "/" Then
                                XL.Cells(lineNumber, 4).Value = currentLine
                            Else
                                XL.Cells(lineNumber, 4).Value = Left(currentLine, Len(currentLine) - 1)
                            End If
                        End If
    jump:
                    End If
                End If
            Else
            ' the tag is not the end so we keep searching for the next one in the line
            'currentLine = Left(currentLine, nextTagPosition - 1) & Right(currentLine, Len(currentLine) - InStr(currentLine, ">"))
            endOfCurrentTag = InStr(endOfCurrentTag + 1, currentLine, ">")
            GoTo endFinder
            End If
            End If
        Else
        'first we check it is really a new tag (not a comment or a ending tag)
        If (Left(startTag, 1) <> "/" And Left(startTag, 1) <> "!" And startTag <> "APA" And startTag <> "apa") Then
        'pop up window to ask if the tag unknown has to be add to the database
            addFieldPopUp = MsgBox("Unknown name : " & startTag & " Do you want to add it as a field ?", vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Add field ?")
            If (addFieldPopUp = vbYes) Then
                XL.Cells(1, fieldsTotal + 1).Value = startTag
                GoTo addFieldLoop
            End If
        End If
        End If
    Else
    'using the data as the continuation of the previous line
    If continuation Then
    'we will use the very same method than just above to find the tags
        startTag = tagForContinuation
        currentLine = lineForContinuation & " " & Data
        GoTo endFinder
    End If
    End If
    End If
    Loop
    'close File
    Close #1
     
    '******** This must be custumised dependding on the computer in use ********
    ChDrive "C"
    ChDir "C:\"      'you can had a folder. example : "C:\My docs\"
    '***************************************************************************
    DoCmd.SetWarnings False
    '***************************************************************************
    '***************************************************************************
    XL.ThisWorkbook.SaveAs ("Tempfile")
    '***************************************************************************
    '***************************************************************************
    'here the imporation can begin
    'request a name
    EnterName:
    newName = InputBox("Enter a name for the new reference in the database ?", "Name new entry")
    If newName = "" Then
        If MsgBox("You have left the name blank or hit Cancel. It will cancel the process." & Chr(13) & "Do you want to continu ?" & Chr(13) & "Remeber you cannot leave the name blank.", vbExclamation + vbYesNo, "Cancel import ?") = vbYes Then
        GoTo deleteFile
        Else
        GoTo EnterName
        End If
    End If
    If DCount("*", "References_index", "References like '" & newName & "'") <> 0 Then
        If MsgBox("A reference with this name already exists in the database. The reference will not be created, but new records will be added to the existing one." & Chr(13) & "Do you want to continu ?", vbYesNo + vbExclamation, "Existing Reference !") = vbNo Then GoTo EnterName
    End If
    'import the table
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, newName, "C:\Tempfile.xls", True
    'request a name
    newName = InputBox("New Reference Name in the database ?", "Rename new entry")
    'rename the new entry as you wish
    DoCmd.Rename newName, acTable, "New Reference"
    'add the new reference to the references table
    DoCmd.RunSQL ("insert into References_index(References) Values('" & newName & "')")
    MsgBox "New Reference added: " & Chr(13) & newName
    'delete the tempFile.xls
    deleteFile:
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.deleteFile "C:\Tempfile.xls"
    DoCmd.SetWarnings True
    'close XL now because there is no more use
    CloseXL
    DoCmd.Close acForm, "Add reference"
    End Sub
    J ai mis la partie a pb entre etoiles.
    Comme vous le voyew, je sauvegarde uniquement pour pouvoir importer la feuille dans access et apres j efface le fichier. Est ce que y a un qutre moyen de faire plus direcet et sans avoir a enregisterer et effecer un fichier.
    Merci, surtout si vous etes arriver jusque la

  2. #2
    Membre expérimenté
    Avatar de Frank
    Homme Profil pro
    Chef de projet Informatique
    Inscrit en
    Avril 2002
    Messages
    1 095
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Chef de projet Informatique
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Avril 2002
    Messages : 1 095
    Points : 1 392
    Points
    1 392
    Par défaut
    Il n'est pas possible d'intégrer le fichier XML plutôt ?
    cela t'éviterait une étape.

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    74
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 74
    Points : 33
    Points
    33
    Par défaut
    ou prpose tu que j integre le fichier XML. En tout cas, je ne peux pas l importer directement dans access car sa facon de l integrer ne correspond pas du tout a ce qu il faut pour moi.

  4. #4
    Membre expérimenté
    Avatar de Frank
    Homme Profil pro
    Chef de projet Informatique
    Inscrit en
    Avril 2002
    Messages
    1 095
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Chef de projet Informatique
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Avril 2002
    Messages : 1 095
    Points : 1 392
    Points
    1 392
    Par défaut
    Jettes un oeil au tutoriel de Cafeine, tu pourrai trouver une piste interressante, qui sait ?

  5. #5
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    74
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 74
    Points : 33
    Points
    33
    Par défaut
    ben c vrai que ca a pas l air mal...mais je suis pas sur d avoir tout compris.. pour revenir plus precisement a ma demande, y a une raison pour laquelle ca marche pas ?

  6. #6
    Membre expérimenté
    Avatar de Frank
    Homme Profil pro
    Chef de projet Informatique
    Inscrit en
    Avril 2002
    Messages
    1 095
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 56
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Chef de projet Informatique
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Avril 2002
    Messages : 1 095
    Points : 1 392
    Points
    1 392
    Par défaut
    Le problème, c'est que tu ne nous dit pas ce qui ne marche pas...

  7. #7
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    74
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 74
    Points : 33
    Points
    33
    Par défaut
    desole, mais hier j etais absent...sympa le jour de conge a cause de l electricite qui marche pas

    alors en fait ce qui ne marche pas, c la sauvegarde du fichier, j ai une erreur 1004, a savoir que la methode 'ThisWorkbook' of object '_Application' failed

    Voila. J arrive pas a comprendre l erreur en fait et d ou vient le pb.

    Merci d avance

  8. #8
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    74
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 74
    Points : 33
    Points
    33
    Par défaut
    en fait j ai trouve... il faut utiliser ActiveWorkBook

    Voila. Merci bien a tous.

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

Discussions similaires

  1. Sauvegarder un fichier Excel
    Par toutou2000 dans le forum Débuter
    Réponses: 4
    Dernier message: 21/11/2008, 14h08
  2. Sauvegarder un fichier excel
    Par debvba dans le forum Macros et VBA Excel
    Réponses: 13
    Dernier message: 25/09/2007, 15h51
  3. Réponses: 1
    Dernier message: 03/04/2007, 18h31
  4. sauvegarder un fichier excel
    Par Pitou5464 dans le forum Access
    Réponses: 3
    Dernier message: 28/08/2006, 16h03
  5. Sauvegarder un fichier Excel
    Par jarod_bx dans le forum Access
    Réponses: 4
    Dernier message: 29/05/2006, 13h40

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