Private Sub Command1_Click()
Dim i As Integer
i = Shell("calc.exe")
If i > 0 Then
MsgBox "执行成功!", vbOKOnly + vbInformation, "系统提示"
End If
End Sub
这是打开windows计算器的代码,因为这是系统自带的,所以只输入了相对路径,如果要改成不是系统自带的程序,需要输入绝对路径
函数:Call Shell(PathName,WindowStyle)
或 a = Shell(PathName,WindowStyle) ,不需要声明.
注解:PathName 可以是程序路径(如相对路径 App.path +"\...\*.exe")、Windows 内置代号(如控制面板选项:Appwiz.cpl 即为添加删除程序,不过要结合 RunDLL32);WindowStyle 是窗口初始样式,如最大化、最小化等。
示例:
Private Sub Command1_Click()
Call Shell("c:\windows\explorer.exe", vbNormalFocus)
End Sub
提示消息的很简单了Msgbox"已调用",vbinformation
,加在 Call Shell("c:\windows\explorer.exe", vbNormalFocus)
后
Private Declare Function OpenProcess Lib "kernel32" _
( _
ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long _
) As Long
Private Declare Function CloseHandle Lib "kernel32" _
( _
ByVal hObject As Long _
) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long _
) As Long
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFFFFFF
Private Sub Command1_Click()
'Dim pId As Long
'声明pId变量存储Process Id
Dim pHnd As Long
'声明pHnd变量存储Process Handle
'pId = Shell("Notepad", vbNormalFocus)
'Shell传回Process Id
'pHnd = OpenProcess(SYNCHRONIZE, 0, pId)
'取得 Process Handle
pHnd = Shell_GetHnd("Notepad", vbNormalFocus)
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE)
'无限等待,直到程序结束
Call CloseHandle(pHnd)
End If
MsgBox ("记事本已经关闭!")
End Sub
'参数PathName 就是你要运行的程序的名称
Private Function Shell_GetHnd(ByVal PathName As String, _
Optional WindowStyle As VbAppWinStyle = vbMaximizedFocus, _
Optional pID As Long) As Long
pID = Shell(PathName, WindowStyle)
Shell_GetHnd = OpenProcess(SYNCHRONIZE, 0, pID)
End Function
执行外部程序不难,但是想知道外部程序是否结束就比较麻烦,可能需要进程查看的方法,具体你去查资料。
做弹窗,button里做script