Attendre pour la Coquille à la fin, puis format de cellule - synchrone exécuter une commande

J'ai un exécutable que j'appel à l'aide de la commande shell:

Shell (ThisWorkbook.Path & "\ProcessData.exe")

L'exécutable ne certains calculs, ensuite l'exportation des résultats vers Excel. Je veux être en mesure de changer le format des résultats APRÈS ils sont exportés.

En d'autres termes, j'ai besoin de la commande du Shell d'abord ATTENDRE jusqu'à ce que l'exécutable termine sa tâche, exporte les données, et ENSUITE faire la prochaine commandes de format.

J'ai essayé le Shellandwait(), mais sans beaucoup de chance.

J'ai eu:

Sub Test()

ShellandWait (ThisWorkbook.Path & "\ProcessData.exe")

'Additional lines to format cells as needed

End Sub

Malheureusement, encore, la mise en forme prend place avant de l'exécutable est terminée.

Juste pour référence, voici mon code complet à l'aide de ShellandWait

' Start the indicated program and wait for it
' to finish, hiding while we wait.


Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Const INFINITE = &HFFFF


Private Sub ShellAndWait(ByVal program_name As String)
Dim process_id As Long
Dim process_handle As Long

' Start the program.
On Error GoTo ShellError
process_id = Shell(program_name)
On Error GoTo 0

' Wait for the program to finish.
' Get the process handle.
process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
If process_handle <> 0 Then
WaitForSingleObject process_handle, INFINITE
CloseHandle process_handle
End If

Exit Sub

ShellError:
MsgBox "Error starting task " & _
txtProgram.Text & vbCrLf & _
Err.Description, vbOKOnly Or vbExclamation, _
"Error"

End Sub

Sub ProcessData()

  ShellAndWait (ThisWorkbook.Path & "\Datacleanup.exe")

  Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlTop
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
End Sub
  • Si vous essayez le code complet sur: http://www.cpearson.com/excel/ShellAndWait.aspx
InformationsquelleAutor Alaa Elwany | 2012-01-17