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 :

Macro qui teste si un programme externe est en cours d'execution


Sujet :

Macros et VBA Excel

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 4
    Points : 3
    Points
    3
    Par défaut Macro qui teste si un programme externe est en cours d'execution
    Bonjour,

    J'ai lu cette discussion mais ça ne marche plus avec Access 2013 64 bits.
    Rien ne se passe, la commande n'est pas exécutée
    Pourquooooooooi ?

  2. #2
    Expert éminent sénior
    Avatar de Marc-L
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2013
    Messages
    9 468
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2013
    Messages : 9 468
    Points : 18 674
    Points
    18 674
    Par défaut

    Bonjour,

    normal car la déclaration d'API est différente en 64 bits !

    A lire : Développer avec Office 64 bits

  3. #3
    Invité
    Invité(e)
    Par défaut Bonjour, dans le même esprits
    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
    #If win64 Then
        Private Declare PtrSafe Function WaitForSingleObject Lib "kernel32" _
       (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
     
    Private Declare PtrSafe Function CloseHandle Lib "kernel32" _
       (ByVal hObject As Long) As Long
     
    Private Declare PtrSafe Function OpenProcess Lib "kernel32" _
       (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessId As Long) As Long
    #Else
    Private Declare Function WaitForSingleObject Lib "kernel32" _
       (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
     
    Private Declare Function CloseHandle Lib "kernel32" _
       (ByVal hObject As Long) As Long
     
    Private Declare Function OpenProcess Lib "kernel32" _
       (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessId As Long) As Long
    #End If
     
    Private Const INFINITE = -1&
    Private Const SYNCHRONIZE = &H100000
     
    Private Sub Command1_Click()
        Dim iTask As Long, ret As Long, pHandle As Long
        iTask = Shell("notepad.exe", vbNormalFocus)
        pHandle = OpenProcess(SYNCHRONIZE, False, iTask)
        ret = WaitForSingleObject(pHandle, INFINITE)
        ret = CloseHandle(pHandle)
        MsgBox "Process Finished! Thank you"
    End Sub

  4. #4
    Expert éminent
    Homme Profil pro
    Inscrit en
    Août 2010
    Messages
    3 453
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Août 2010
    Messages : 3 453
    Points : 6 871
    Points
    6 871
    Par défaut
    Bonjour et bonne année,

    Une piste :
    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
     
    Public Declare Function GetWindowText _
                   Lib "user32" _
                   Alias "GetWindowTextA" ( _
                   ByVal hWnd As Long, _
                   ByVal lpString As String, _
                   ByVal cch As Long) As Long
     
    Public Declare Function EnumWindows _
                   Lib "user32" ( _
                   ByVal lpEnumFunc As Long, _
                   ByVal lParam As Long) As Long
     
    Dim Ouvert As String
    Dim NomProg As String
     
    'proc de test, à adapter :
    Sub Test()
     
        Dim lResult As Long
     
        NomProg = "Excel" 'cherche si Excel est ouvert
     
        lResult = EnumWindows(AddressOf ProgrammeOuvert, 0&)
     
        MsgBox Ouvert
     
    End Sub
     
    'cherche dans le titre des fenêtres le nom du programme
    Public Function ProgrammeOuvert(ByVal hWnd As Long, _
                                    ByVal lgParam As Long) As Long
     
        Dim Buffer As String
        Dim Result As Long
     
        Buffer = Space(255)
     
        Result = GetWindowText(hWnd, Buffer, 255)
     
        If Left(Buffer, 1) <> Chr(0) Then
     
            If InStr(UCase(Trim(Buffer)), UCase(NomProg)) <> 0 Then
     
                Ouvert = NomProg & " est ouvert !"
     
                Exit Function
     
            Else
     
                Ouvert = NomProg & " est fermé !"
     
            End If
     
        End If
     
        ProgrammeOuvert = 1
     
    End Function
    Hervé.

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2013
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2013
    Messages : 4
    Points : 3
    Points
    3
    Par défaut Problème résolu
    Effectivement, en 64bit l'appel aux API est différent.
    Le code ci dessous fonctionne: lancement de l'appli + attente de fermeture.
    Cerise sur le gâteau: un paramètre permet d'afficher ou de masquer la fenêtre (0 = show, 5 = hide). Indispensable pour exécuter une boucle de batchs en tache de fond, sans parasiter l'affichage et le focus.

    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
    Private Const STARTF_USESHOWWINDOW& = &H1
    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const INFINITE = -1&
     
    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 PtrSafe Function WaitForSingleObject Lib "kernel32" (ByVal _
        hHandle As Long, ByVal dwMilliseconds As Long) As Long
     
    Private Declare PtrSafe 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 PtrSafe Function CloseHandle Lib "kernel32" (ByVal _
        hObject As Long) As Long
     
    Public Sub ShellWait(Pathname As String, Optional WindowStyle As Long)
        Dim proc As PROCESS_INFORMATION
        Dim start As STARTUPINFO
        Dim ret As Long
        ' Initialize the STARTUPINFO structure:
        With start
            .cb = Len(start)
            If Not IsMissing(WindowStyle) Then
                .dwFlags = STARTF_USESHOWWINDOW
                .wShowWindow = WindowStyle
            End If
        End With
        ' Start the shelled application:
        ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
                NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
        ' Wait for the shelled application to finish:
        ret& = WaitForSingleObject(proc.hProcess, INFINITE)
        ret& = CloseHandle(proc.hProcess)
    End Sub
    Merci à tous !

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

Discussions similaires

  1. Réponses: 3
    Dernier message: 23/04/2010, 18h06
  2. [Toutes versions] macro qui teste si un programme externe est en cours d'execution
    Par jejerome dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 11/09/2009, 20h50
  3. Macro qui teste la colonne ou se trouve le bouton ?
    Par Drvibe dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 23/02/2008, 21h01
  4. Savoir si un programme externe est déjà lancé
    Par _iri_ dans le forum Général Python
    Réponses: 4
    Dernier message: 08/10/2006, 18h09
  5. faire un test sur un programme externe
    Par marieheraud dans le forum Windows
    Réponses: 3
    Dernier message: 02/09/2004, 18h32

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