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 :

lancer une commande dos en shell


Sujet :

Macros et VBA Excel

  1. #1
    Membre habitué
    Inscrit en
    Novembre 2007
    Messages
    293
    Détails du profil
    Informations forums :
    Inscription : Novembre 2007
    Messages : 293
    Points : 125
    Points
    125
    Par défaut lancer une commande dos en shell
    bonjour forum

    je bute depuis un momment pour lancer un fichier de commande dos depuis vba
    j' ai essayé ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    Sub maj_IVM()
    com_dos = ThisWorkbook.Path & "\Rep_IVM.cmd"
    S = ThisWorkbook.Path & "\Rep_IVM.txt"
    com_dos = "COMMAND.COM /C " & com_dos
    Shell "cmd.exe /c " & com_dos
    End Sub
    Rep_IVM.cmd contient :
    dir S:\ORG_CIS_IVM /s >Rep_IVM.txt

    ce code ne veut pas se lancer

    j' aimerai essayer

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    Sub try1()
     
    com_dos = ThisWorkbook.Path & "\Rep_IVM.cmd"
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run com_dos, SH_WIDE
     
    End Sub
    mais la compilation plante :
    "object required"
    peut être y a t il une librairie qu'il faille activer avant
    mais laquelle ?

  2. #2
    Membre chevronné Avatar de aalex_38
    Inscrit en
    Septembre 2007
    Messages
    1 631
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 1 631
    Points : 1 999
    Points
    1 999
    Par défaut
    bonjour à tous,

    Un exemple :

    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
    Sub essai_forum_Bat()
    Dim tableau(3)
     
    iNumFichier = FreeFile
    Open ThisWorkbook.Path & "\Test.bat" For Output As iNumFichier
     
        tableau(1) = "S"
        tableau(2) = "CD S:\ORG_CIS_IVM\"
        tableau(3) = "dir /S >Rep_IVM.txt"
     
    For j = 1 To UBound(tableau)
        Print #iNumFichier, tableau(j) ''& Chr$(34)
    Next j
     
    Close iNumFichier
    lShell = Shell(ThisWorkbook.Path & "\Test.bat", vbMinimizedFocus)
     
    End Sub

  3. #3
    Membre habitué
    Inscrit en
    Novembre 2007
    Messages
    293
    Détails du profil
    Informations forums :
    Inscription : Novembre 2007
    Messages : 293
    Points : 125
    Points
    125
    Par défaut
    ok merci
    ton code marche

    peux tu me dire comment on sait par vba que la commande dos
    - ne s'exécute pas
    -si elle s'exécute et quand elle est terminée

    mon pb est que le répertoire que je scrute est trés grand donc le batch est trés long et j'aimerai pouvoir exploiter les données que lorsque le fichier généré est disponible

  4. #4
    Membre chevronné Avatar de aalex_38
    Inscrit en
    Septembre 2007
    Messages
    1 631
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 1 631
    Points : 1 999
    Points
    1 999
    Par défaut
    le batch est trés long
    Pour controler la fin d'une application je ne connais pas de réponse simple :

    vba ShellAndWait

  5. #5
    Membre habitué
    Inscrit en
    Novembre 2007
    Messages
    293
    Détails du profil
    Informations forums :
    Inscription : Novembre 2007
    Messages : 293
    Points : 125
    Points
    125
    Par défaut
    bon j'ai vu des choses mais les codes proposés passent par
    WScript.Shell
    ça fait planter la compil
    il faut je pense activer une librairie dans les références et je ne sais pas laquelle

  6. #6
    Membre averti
    Profil pro
    Inscrit en
    Février 2006
    Messages
    288
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 288
    Points : 364
    Points
    364
    Par défaut
    Bonjour,
    Voici un code (pas de moi) qui permet au code VB d'attendre la fin du batch sans avoir à en contrôler la fin par d'autres moyens.

    Il faut mettre ce code dans un module à part :
    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
     
    Private Type STARTUPINFO
             cb As Long
             lpReserved As String
             lpDesktop As String
             lpTitle As String
             dwX As Long
             dwY As Long
             dwXSize As Long
             dwYSize As Long
             dwXCountChars As Long
             dwYCountChars As Long
             dwFillAttribute As Long
             dwFlags As Long
             wShowWindow As Integer
             cbReserved2 As Integer
             lpReserved2 As Long
             hStdInput As Long
             hStdOutput As Long
             hStdError As Long
    End Type
     
    Private Type PROCESS_INFORMATION
             hProcess As Long
             hThread As Long
             dwProcessID As Long
             dwThreadID As Long
    End Type
     
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
             hHandle As Long, ByVal dwMilliseconds As Long) As Long
     
    Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
             lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
             lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
             ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
             ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
             lpStartupInfo As STARTUPINFO, lpProcessInformation As _
             PROCESS_INFORMATION) As Long
     
    Private Declare Function CloseHandle Lib "kernel32" (ByVal _
             hObject As Long) As Long
     
    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const INFINITE = -1&
     
    Private Declare Function OpenProcess Lib "kernel32" _
        (ByVal dwDesiredAccess As Long, _
        ByVal bInheritHandle As Long, _
        ByVal dwProcessID As Long) As Long
     
    Private Declare Function GetExitCodeProcess Lib "kernel32" _
        (ByVal hProcess As Long, _
        lpExitCode As Long) As Long
     
     
     
    Public Sub ShellPatient(vCommand As String)
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim ReturnValue As Integer
     
     
    ReturnValue = CreateProcessA(0&, vCommand, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    Do
        ReturnValue = WaitForSingleObject(proc.hProcess, 0)
        DoEvents
        DoEvents
    Loop Until ReturnValue <> 258
     
    ReturnValue = CloseHandle(proc.hProcess)
     
    End Sub
    Puis lancer ta commande DOS comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ShellPatient "cmd.exe /c " & com_dos

  7. #7
    Membre habitué
    Inscrit en
    Novembre 2007
    Messages
    293
    Détails du profil
    Informations forums :
    Inscription : Novembre 2007
    Messages : 293
    Points : 125
    Points
    125
    Par défaut
    ouf j' ai trouvé la solution en exemple pour ceux que ça intersse voici le 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
     
    Sub try()
    myshell = False
    Dim com_dos As String
    com_dos = ThisWorkbook.Path & "\Rep_IVM.cmd"
    'myshell = Shell(com_dos, 1)
     
    myshell = ShellAndWait(com_dos)
     
    MsgBox "fini ok "
     
    End Sub
     
     
     
    Function ShellAndWait(FileName As String)
    Dim objScript As Object
    Dim ShellApp As Long
     
    On Error GoTo ERR_OpenForEdit
     
    Set objScript = CreateObject("WScript.Shell")
     
    ShellApp = objScript.Run(FileName, 1, True)
    ShellAndWait = True
     
    EXIT_OpenForEdit:
    Exit Function
    ERR_OpenForEdit:
    MsgBox Err.Description
    GoTo EXIT_OpenForEdit
    End Function
    par contre quand la commande plante ça dit rien

    je mets cette discution en résolu
    si quelqu'un sait répondre à la question je suis preneur

    Merci à tous et en trés particulier à aalex

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

Discussions similaires

  1. Lancer une commande dos sans voir la fenêtre
    Par Nicopelo dans le forum Windows
    Réponses: 2
    Dernier message: 07/06/2007, 09h12
  2. Lancer une commande DOS a partir d'un poste UNIX
    Par sam.fet dans le forum Linux
    Réponses: 7
    Dernier message: 19/05/2006, 14h06
  3. lancer une commande dos dans une page ASP
    Par noznoz78 dans le forum ASP
    Réponses: 4
    Dernier message: 13/02/2006, 19h06
  4. [system] lancer une commande dos
    Par romuluslepunk dans le forum API standards et tierces
    Réponses: 4
    Dernier message: 20/08/2005, 11h54
  5. [Dos] Lancer une commande dos sans attendre la fin
    Par MrEddy dans le forum Windows
    Réponses: 4
    Dernier message: 09/06/2005, 17h38

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