Invece di utilizzare un messaggio di attesa potresti utilizzare la ShellAndWait. Questa funzione consente di avviare, dall'interno del proprio programma, un'applicazione esterna ed attendere che l'esecuzione termini o venga chiusa, prima di procedere con le istruzioni che seguono.
'Dichiarazioni da inserire in un Modulo bas:
codice:
Public 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
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
' dichiarazioni API
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public 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
Public Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&
'subroutine per lanciare un programma in sincrono da
'inserire sempre nel Modulo bas
'Il programma lanciato viene 'atteso' per
'tutta la sua durata.
Public Sub ShellAndWait(ByVal RunProg As String)
Dim RetVal As Long
Dim proc As PROCESS_INFORMATION
Dim StartInf As STARTUPINFO
StartInf.cb = Len(StartInf)
'Crea sessione del programma da lanciare
RetVal = CreateProcessA(0&, RunProg, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, StartInf, proc)
RetVal = WaitForSingleObject(proc.hProcess, INFINITE)
RetVal = CloseHandle(proc.hProcess)
End Sub
'------------------------------
'In questo esempio sul form alla pressione del tasto di comando viene aperto il file.txt con notepad:
codice:
Private Sub Command1_Click()
XFile = "c:\File.txt"
If Dir(XFile) <> "" Then
Call ShellAndWait("notepad.exe " & XFile & "")
Else
MsgBox "Nessun " & XFile, vbExclamation
End If
End Sub