概要

您可以从 Visual Basic for Applications 宏以启动 Microsoft Windows 下的另一个程序调用ShellExecute() Windows API 函数。使用ShellExecute()而不是外壳(Visual Basic 语句) 或WinExec() (Windows API 函数) 来解决后者命令以下限制:外壳和WinExec(),不能通过指定一个文件名称来启动应用程序。例如,下面的Shell语句将会失败:

x = Shell("C:\My Documents\Book1.Xls")

详细信息

Microsoft 提供的编程示例仅用于说明,没有任何明示或暗示的担保。这包括但不限于适销性或特定用途适用性的暗示担保。本文假定您熟悉所演示的编程语言和用于创建和调试过程的工具。Microsoft 的支持工程师可以帮助解释某个特定过程的功能,但是他们不会修改这些示例以提供额外的功能或构建过程以满足您的特定要求。以下是示例 Visual Basic for Applications 调用ShellExecute() Windows API 函数的宏。ShellExecute()确定是否已在运行 Microsoft Excel;如果是这样,它将 Book1.xls 加载到当前的 Microsoft Excel 会话。如果尚未运行 Microsoft Excel, ShellExecute()将启动 Microsoft Excel,并加载 Book1.xls。

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 LongDeclare Function apiFindWindow Lib "User32" Alias "FindWindowA" _   (ByVal lpclassname As Any, ByVal lpCaption As Any) As LongGlobal Const SW_SHOWNORMAL = 1Sub ShellExecuteExample()   Dim hwnd   Dim StartDoc   hwnd = apiFindWindow("OPUSAPP", "0")   StartDoc = ShellExecute(hwnd, "open", "C:\My Documents\Book1.xls", "", _      "C:\", SW_SHOWNORMAL)End Sub

ShellExecute()函数打开或打印指定的文件。以下是有关信息 ShellExecute() 901 904 页从 Microsoft Windows 软件开发工具包 (SDK)"程序员参考,卷 2:Functions。"

参数

                Parameter            Description---------------------------------------------------------------------------   hwnd                 Identifies the parent window.   lpszOp               A string specifying the operation to perform. This                        string can be "open" or "print".   lpszFile             Points to a string specifying the file to open.   lpszParams           Points to a string specifying parameters passed to                        the application when the lpszFile parameter                        specifies an executable file. If lpszFile points to                        a string specifying a document file, this parameter                        is NULL.   lpszDir              Points to a string specifying the default                        directory.   fsShowCmd            Specifies whether the application window is to be                        shown when the application is opened. This                        parameter can be one of the following values:      Value          Meaning      ---------------------------------------------------------------------      0              Hides the window and passes activation to another                     window.      1              Activates and displays a window. If the window is                     minimized or maximized, Windows restores it to its                     original size and position (same as 9).      2              Activates a window and displays it as an icon.      3              Activates a window and displays it as a maximized                     window.      4              Displays a window in its most recent size and                     position. The window that is currently active remains                     active.      5              Activates a window and displays it in its current                     size and position.      6              Minimizes the specified window and activates the                     top-level window in the system's list.      7              Displays a window as an icon. The window that is                     currently active remains active.      8              Displays a window in its current state. The window                     that is currently active remains active.      9              Activates and displays a window. If the window is                     minimized or maximized, Windows restores it to its                     original size and position (same as 1).

返回

如果函数成功,返回值是已打开或打印时,该应用程序的实例句柄。(此句柄也可能是该句柄的 DDE 服务器应用程序。)返回值小于或等于 32 指定错误。

错误

ShellExecute()函数返回值 31,如果没有为指定的文件类型关联,或者如果没有关联的文件类型中指定的操作。其他可能的错误值如下所示:Value Meaning--------------------------------------------------------------------------- 0 System was out of memory, executable file was corrupt, or relocations were invalid. 2 File was not found. 3 Path was not found. 5 Attempt was made to dynamically link to a task, or there was a sharing or network-protection error. 6 Library required separate data segments for each task. 8 There was insufficient memory to start the application. 10 Windows version was incorrect. 11 Executable file was invalid. Either it was not a Windows application, or there was an error in the .exe image. 12 Application was designed for a different operating system. 13 Application was designed for MS-DOS 4.0. 14 Type of executable file was unknown. 15 Attempt was made to load a real-mode application (developed for an earlier version of Windows). 16 Attempt was made to load a second instance of an executable file containing multiple data segments that were not marked read-only. 19 Attempt was made to load a compressed executable file. The file must be decompressed before it can be loaded. 20 Dynamic-link library (DLL) file was invalid. One of the DLLs required to run this application was corrupt. 21 Application requires Microsoft Windows 32-bit extensions.

注释

由 lpszFile 参数指定的文件可以是文档文件或可执行文件。如果是文档文件,此函数打开或打印它,这取决于 lpszOp 参数的值。如果它是一个可执行文件,此函数会打开它,即使由 lpszOp 指向的字符串"打印"。

参考资料

有关如何使用本文中示例代码的详细信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

212536 OFF2000: 如何运行知识库文章中的示例代码有关获取帮助 Visual Basic for Applications 的详细信息,请参阅下面的 Microsoft 知识库文章:

226118 OFF2000: Visual Basic for Applications 的编程资源Microsoft Windows SDK"程序员参考,卷 2: 功能,"901 904 页

需要更多帮助?

需要更多选项?

了解订阅权益、浏览培训课程、了解如何保护设备等。