Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongFunction Shell(Program As String, Optional ShowCmd As Long = vbNormalNoFocus, Optional ByVal WorkDir As Variant) As Long Dim FirstSpace As Integer, Slash As Integer If Left(Program, 1) = """" Then FirstSpace = InStr(2, Program, """") If FirstSpace <> 0 Then Program = Mid(Program, 2, FirstSpace - 2) & Mid(Program, FirstSpace + 1) FirstSpace = FirstSpace - 1 End If Else FirstSpace = InStr(Program, " ") End If If FirstSpace = 0 Then FirstSpace = Len(Program) + 1 If IsMissing(WorkDir) Then For Slash = FirstSpace - 1 To 1 Step -1 If Mid(Program, Slash, 1) = "\" Then Exit For Next If Slash = 0 Then WorkDir = CurDir ElseIf Slash = 1 Or Mid(Program, Slash - 1, 1) = ":" Then WorkDir = Left(Program, Slash) Else WorkDir = Left(Program, Slash - 1) End If End If Shell = ShellExecute(0, vbNullString, _ Left(Program, FirstSpace - 1), LTrim(Mid(Program, FirstSpace)), _ WorkDir, ShowCmd) If Shell < 32 Then VBA.Shell Program, ShowCmd 'To raise Error End Function Call Shell(filename) ' filename 为所调用文件 路径,什么文件都可以打开 ,filename为字符型变量
就可以了,其中str不一定要是可执行文件,例如doc文档也可以,都可以自动关联打开
把分数给我吧
就可以了,其中str不一定要是可执行文件,例如doc文档也可以,都可以自动关联打开
把分数给我吧
通过带一些参数,可以使屏幕出现或不出现执行窗口。
另外通过其他函数可以一直探测执行外部程序的情况。
If Left(Program, 1) = """" Then
FirstSpace = InStr(2, Program, """")
If FirstSpace <> 0 Then
Program = Mid(Program, 2, FirstSpace - 2) & Mid(Program, FirstSpace + 1)
FirstSpace = FirstSpace - 1
End If Else
FirstSpace = InStr(Program, " ")
End If If FirstSpace = 0 Then FirstSpace = Len(Program) + 1
If IsMissing(WorkDir) Then
For Slash = FirstSpace - 1 To 1 Step -1
If Mid(Program, Slash, 1) = "\" Then Exit For
Next If Slash = 0 Then
WorkDir = CurDir
ElseIf Slash = 1 Or Mid(Program, Slash - 1, 1) = ":" Then
WorkDir = Left(Program, Slash)
Else
WorkDir = Left(Program, Slash - 1)
End If End If Shell = ShellExecute(0, vbNullString, _
Left(Program, FirstSpace - 1), LTrim(Mid(Program, FirstSpace)), _
WorkDir, ShowCmd)
If Shell < 32 Then VBA.Shell Program, ShowCmd 'To raise Error
End Function
Call Shell(filename) ' filename 为所调用文件 路径,什么文件都可以打开 ,filename为字符型变量