概要
ShellExecute() Windows API 関数は、別のプログラムで、Microsoft Windows を起動するマクロを Visual Basic for Applications から呼び出すことができます。ShellExecute()をシェル(Visual Basic のステートメントの場合) またはWinExec() (Windows API 関数) ではなく後者のコマンドの次の制限を回避するために使用します。シェルとWinExec()ファイル名のみを指定することによってアプリケーションを起動することはできません。たとえば、次のシェルステートメントは失敗します。
x = Shell("C:\My Documents\Book1.Xls")
詳細
マイクロソフトでは、プログラミングの例だけで明示または黙示の保証もないです。ここで言う保証とは、特定の目的に対する商品性や適合性の暗示的保証を含んでいますが、それに限定されるわけではありません。この資料では、例示されているプログラミング言語およびプロシージャの作成やデバッグに使用するツールにお客様が精通していることを前提としています。マイクロソフトのサポート エンジニアは、特定のプロシージャの機能を説明することができますが、機能の追加またはお客様固有の要件を満たすようにプロシージャを作成するこれらの例は変更されません。 ShellExecute() Windows API 関数を呼び出すマクロのサンプル Visual Basic for Applications を次に示します。ShellExecute()は、Microsoft Excel が既に実行されているかどうかを決定します。場合は、Book1.xls が現在の 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 で指す場合も。
関連情報
この資料のサンプル コードを使用する方法については、マイクロソフト サポート技術情報の記事を表示するのには以下の記事番号をクリックします。
212536 [off2000]: サポート技術情報のサンプル コードを実行する方法Visual Basic for Applications のヘルプの詳細については、次のマイクロソフト サポート技術情報資料を参照してください。
226118 off 2000: Visual Basic for Applications のプログラミング リソースMicrosoft Windows SDK「プログラマーズ リファレンス、ボリューム 2: 関数は、"ページ 901-904