其实不难,
介绍两种打开文件的方式,这两种方式都是我在编程序中常用的,绝对可用
1.简单的方式(以资源管理器方式打开,但是会有讨厌的提示)
StrFileName=App.Path & "\abc\abc.exe"
Shell "EXPLORER.EXE /n," & StrFileName, vbNormalFocus
2.高效的方式(使用API,推荐使用)
StrFileName=App.Path & "\abc\abc.exe"
Call SubOpenFile(StrFileName,form1 )
其中form1是调用命令所用的窗体名,根据实际情况来写
请将下列内容复制到你的程序模块中,
那么你就可以使用上面语句打开你说的文件了
'注意在引用该FSO功能时先引用 Microsoft Scripting Runtime
Public ObjFso As New FileSystemObject
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 Long
Public Sub SubOpenFile(ByVal FileSaveName As String, ByVal StrFrmName As Form)
'打开文件
'输入:sFileName 待打开文件的全路径加上文件名,如 "F:\VB\RunExe\Module1.bas"
'输入:调用该子程序的窗体名称
If Not ObjFso.FileExists(FileSaveName) Then
MsgBox ("没有找到要打开的文件")
Exit Sub
End If
sCorrectPath = ObjFso.GetParentFolderName(FileSaveName)
sFileName = ObjFso.GetFileName(FileSaveName)
'输入:调用该子程序的窗体名称
lHwnd = StrFrmName.hwnd
'lShellFile = shellExecute(lHwnd, "open", FileSaveName, vbNullString, sCorrectPath, SW_SHOWNORMAL)
lShellFile = shellExecute(lHwnd, "open", FileSaveName, vbNullString, sCorrectPath, 5)
'错误处理
If lShellFile > 32 Then
Exit Sub
Else
Select Case lShellFile
Case 2
If Right(sFileName, 3) <> "htm" Then
MsgBox "File Not Found.", vbCritical + vbOKOnly, "X-File:"
End If
Exit Sub
Case 3
MsgBox "Path not Found.", vbCritical + vbOKOnly, "X-File:"
Exit Sub
Case 5
MsgBox "Access denied.", vbCritical + vbOKOnly, "X-File:"
Exit Sub
Case 8
MsgBox "Out of Memory.", vbCritical + vbOKOnly, "X-File:"
Exit Sub
Case 32
MsgBox "Shell32.dll not Found.", vbCritical + vbOKOnly, "X-File:"
Exit Sub
End Select
End If
End Sub
温馨提示:答案为网友推荐,仅供参考