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

VB 6 et antérieur Discussion :

Ouvrir explorateur pour choisir fichier excel


Sujet :

VB 6 et antérieur

  1. #1
    Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 89
    Points : 56
    Points
    56
    Par défaut Ouvrir explorateur pour choisir fichier excel
    Bonjour a tous,

    Je veux ajuster un code afin de pouvoir choisir le fichier excel que je veux ouvrir sur mon ordinateur.

    En ce moment, je dois donner le chemin exacte, mais je veux qu'il m'ouvre l'explorateur afin de choisir un fichier excel.

    Comment faire s.v.p?

    Voici 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
    Dim xlObject    As Excel.Application
    Dim xlWB        As Excel.Workbook
     
        Set xlObject = New Excel.Application
     
       Set xlWB = xlObject.Workbooks.Open("C:\Documents and Settings\xxxxxxx\Desktop\Order Template.xls") 'Open your book here
     
       Clipboard.Clear
       With xlObject.ActiveWorkbook.ActiveSheet
           .Range("A1:z200").Copy 'Set selection to Copy
       End With
       With MSFlexGrid1
           .Redraw = False     'Dont draw until the end, so we avoid that flash
           .Row = 0            'Paste from first cell
           .Col = 0
           .RowSel = .Rows - 1 'Select maximum allowed (your selection shouldnt be greater than this)
           .ColSel = .Cols - 1
           .Clip = Replace(Clipboard.GetText, vbNewLine, vbCr) 'Replace carriage return with the correct one
           .Col = 1            'Just to remove that blue selection from Flexgrid
           .Redraw = True      'Now draw
       End With
     
       xlObject.DisplayAlerts = False 'To avoid "Save woorkbook" messagebox
       'Close Excel
       xlWB.Close
       xlObject.Application.Quit
       Set xlWB = Nothing
        Set xlObject = Nothing
    Merci pour votre aide.

  2. #2
    Expert éminent sénior
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 107
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 107
    Points : 16 633
    Points
    16 633
    Par défaut
    Dans ton editeur VB, Ctrl+T => Contrôle Microsoft CommonDialog 6.0 (COMDLG32.OCX)
    Ce composant permet d'explorer ton disque dur (ou autre unité de stockage).
    Pour des informations d'utilisaton, F2, voir F1 pour plus d'expliquations.
    rechercher dans ce forum avec le mot CommonDialog il y a plein d'exemples

  3. #3
    Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 89
    Points : 56
    Points
    56
    Par défaut
    Super, je suis rendu à un étape plus loin grace à ton aide.

    Maintenant que je peux ouvrir la boite de dialog OUVRIR, comment je peux sélectionné le 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
    Private Sub Command1_Click()
    
    Dim xlObject    As Excel.Application
    Dim xlWB        As Excel.Workbook
    
    CommonDialog1.ShowOpen
    
        Set xlObject = New Excel.Application
    
       Set xlWB = xlObject.Workbooks.Open("C:\Documents and Settings\jpoitra\Desktop\Floral Order Template.xls") 'Open your book here
                  
       Clipboard.Clear
       With xlObject.ActiveWorkbook.ActiveSheet
           .Range("A1:z200").Copy 'Set selection to Copy
       End With
       With MSFlexGrid1
           .Redraw = False     'Dont draw until the end, so we avoid that flash
           .Row = 0            'Paste from first cell
           .Col = 0
           .RowSel = .Rows - 1 'Select maximum allowed (your selection shouldnt be greater than this)
           .ColSel = .Cols - 1
           .Clip = Replace(Clipboard.GetText, vbNewLine, vbCr) 'Replace carriage return with the correct one
           .Col = 1            'Just to remove that blue selection from Flexgrid
           .Redraw = True      'Now draw
       End With
          
       xlObject.DisplayAlerts = False 'To avoid "Save woorkbook" messagebox
       'Close Excel
       xlWB.Close
       xlObject.Application.Quit
       Set xlWB = Nothing
        Set xlObject = Nothing
    End Sub
    Merci encore pour ton aide

  4. #4
    Membre expert Avatar de OhMonBato
    Homme Profil pro
    Inscrit en
    Mars 2007
    Messages
    2 660
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2007
    Messages : 2 660
    Points : 3 685
    Points
    3 685
    Par défaut
    Citation Envoyé par ProgElecT Voir le message
    Pour des informations d'utilisaton, F2, voir F1 pour plus d'expliquations.
    rechercher dans ce forum avec le mot CommonDialog il y a plein d'exemples
    As tu suivi le conseil de ProgElect ?

  5. #5
    Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 89
    Points : 56
    Points
    56
    Par défaut
    Citation Envoyé par avyrex Voir le message
    Super, je suis rendu à un étape plus loin grace à ton aide.
    Comme mentionné plus haut, oui. Mais je suis bloqué à cette étape. C'est pour ça que je demande encore de l'aide à cette étape ci.

  6. #6
    Expert éminent sénior
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 107
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 107
    Points : 16 633
    Points
    16 633
    Par défaut
    Salut OhMonBato
    Est oui F2 et F1 toivent être le premier reflex, intéresse toi aux propriétés FileName et FileTitle.
    Je peux te dire qu'a la suite d'une sélection par l'utilisateur, la propriété FileName <> vbNullString si l'utilisateur n'a pas utilisé le bouton "Annuler"
    Que
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Left(CommonDialog1.FileName, Len(CommonDialog1.FileName) - Len(CommonDialog1.FileTitle) - 1)
    te permets de récupérer le chemin ou a été sélectionné le fichier.

    Motif de l'edit:
    Si tu n'as pas MSDN (en francais), tu peux le charger ICI

  7. #7
    Membre du Club
    Inscrit en
    Novembre 2006
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 89
    Points : 56
    Points
    56
    Par défaut
    Merci à tous pour votre aide.

    Voici le verdicte final:

    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
    Dim xlObject    As Excel.Application
    Dim xlWB        As Excel.Workbook
    On Error GoTo MyErrHandler
     
        With CommonDialog1
            .CancelError = True
            .Filter = "Microsoft Excel files (*.xls)|*.xls"
            .InitDir = App.Path
            .ShowOpen
            If Not .FileName = "" Then
                Set xlObject = New Excel.Application
                Set xlWB = xlObject.Workbooks.Open(.FileName)
              LakeShore.Command2.Visible = True
       Clipboard.Clear
     
       With xlObject.ActiveWorkbook.ActiveSheet
           .Range("A1:z200").Copy 'Set selection to Copy
       End With
       With MSFlexGrid1
           .Redraw = False     'Dont draw until the end, so we avoid that flash
           .Row = 0            'Paste from first cell
           .Col = 0
           .RowSel = .Rows - 1 'Select maximum allowed (your selection shouldnt be greater than this)
           .ColSel = .Cols - 1
           .Clip = Replace(Clipboard.GetText, vbNewLine, vbCr) 'Replace carriage return with the correct one
           .Col = 1            'Just to remove that blue selection from Flexgrid
           .Redraw = True      'Now draw
       End With
     
       xlObject.DisplayAlerts = False 'To avoid "Save woorkbook" messagebox
       'Close Excel
       xlWB.Close
       xlObject.Application.Quit
       Set xlWB = Nothing
        Set xlObject = Nothing
     
     
     
     
         End If
        End With
     
        Exit Sub
     
     
     
    MyErrHandler:
        Err.Clear

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

Discussions similaires

  1. Réponses: 13
    Dernier message: 06/03/2017, 09h47
  2. [VBA-E] Macro pour tous fichiers Excel ?
    Par belfaigore dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 05/07/2006, 18h25
  3. [VBA-E]une macro unique pour plusieurs fichiers excel
    Par fanchic29 dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 21/04/2006, 16h20
  4. [VB] Ouvrir et modifier un fichier Excel en arrière-fond
    Par truman dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 14/04/2006, 18h49
  5. Ouvrire ou Enregistrer un Fichier Excel
    Par jo281 dans le forum ASP
    Réponses: 1
    Dernier message: 13/12/2005, 18h55

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