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 :

chemin dynamique d'un fichier


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Décembre 2005
    Messages
    41
    Détails du profil
    Informations forums :
    Inscription : Décembre 2005
    Messages : 41
    Par défaut chemin dynamique d'un fichier
    bonjour,
    dans un répertoire, j'ai un fichier excel nommé SynSurestaries.xls qui contient une macro.
    Dans le même répertoire, je copie un ficheir SynSurestaries.sur

    quand j'ouvre le fichier excel, je lance ma macro.
    Le code est le suivant :

    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
    Private Sub Workbook_Open()
    '
    ' OpusCout Macro
    ' Macro enregistrée le 11/12/2007 par EMMANUEL MOTTET
    '
     
    'On supprime toutes les cellules par défaut pour avoir un document vierge
        Cells.Select
        Selection.Delete Shift:=xlUp
     
    '
      Dim chaine As Variant
      chaine = ActiveWorkbook.Path & "\SynSurestaries.sur"
     
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;chaine" _
            , Destination:=Range("A1"))
            .Name = "SynSurestaries"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 1252
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = True
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Range("A1:H1").Select
        Selection.CurrentRegion.Select
        Selection.AutoFormat Format:=xlRangeAutoFormatClassic2, Number:=True, Font _
            :=True, Alignment:=True, Border:=True, Pattern:=True, Width:=True
        Range("A1:P1").Select
        With Selection.Font
            .Name = "Arial"
            .FontStyle = "Normal"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = 6
        End With
        Range("A2:P26").Select
        With Selection.Font
            .Name = "Arial"
            .FontStyle = "Normal"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = 12
        End With
        Range("A27:P27").Select
        With Selection.Font
            .Name = "Arial"
            .FontStyle = "Normal"
            .Size = 10
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = 6
        End With
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        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
        Selection.Borders(xlInsideVertical).LineStyle = xlNone
        With Selection.Interior
            .ColorIndex = 54
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
        End With
    End Sub
    J'ai une erreur 1004 qui correspond au fait qu'il ne trouve pas le fichier.
    si je remplace chaine par le path complet avec le nom du fichier (ex: L:\Projets\Projet OPUS\Projet OPUS 2006\DonneesOPUS 2007\2611lun14h\resultats\SynSurestaries.sur), cela fonctionne.

    Comment faire pour que cela marche quel que soit le répertoire où se trouve le fichier xls et sur?

    merci

  2. #2
    Inactif  
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 054
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 054
    Par défaut
    Bonjour,
    peut-être avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Sub CheminFichier()
    Dim Chaine As String
    Chaine = "TonFichier"
        Dim fs, d, f, s
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.GetFile(Chaine)
        s = UCase(f.Path) & vbCrLf
        MsgBox s, 0, "Chemin complet"
    End Sub
    A+

Discussions similaires

  1. [DOS] Espace dans le chemin et lancement de fichier
    Par nahzhir dans le forum Scripts/Batch
    Réponses: 16
    Dernier message: 20/02/2006, 09h27
  2. Enregistrement d'une variable dynamique dans un fichier
    Par zarbydigital dans le forum Langage
    Réponses: 13
    Dernier message: 24/12/2005, 21h28
  3. Chemin d'accès des fichiers dans des sous rep
    Par Le Veilleur dans le forum C++Builder
    Réponses: 4
    Dernier message: 17/11/2004, 14h37
  4. [langage] chemin absolu d un fichier
    Par nono1782 dans le forum Langage
    Réponses: 4
    Dernier message: 20/07/2004, 14h02
  5. Obtenir le chemin complet d'un fichier
    Par pamplemousse_mk2 dans le forum Linux
    Réponses: 3
    Dernier message: 13/08/2003, 23h07

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