フォーカス(アクティブ)の問題とか、一般にファイルの種類は指定できないので不完全ではありますが、自分でアプリケーションを作成せずに利用する事ができます。 元々、ファイルのアップロードのみに使われるものですからそのような目的でしか利用する事はできません( ファイルの保存用はありません )
' 管理者として実行を強制する Set obj = Wscript.CreateObject("Shell.Application") if Wscript.Arguments.Count = 0 then obj.ShellExecute "wscript.exe", WScript.ScriptFullName & " runas", "", "runas", 1 Wscript.Quit end if Function OpenLocalFileName( ) ' ファイルシステムを操作するオブジェクト Set Fso = WScript.CreateObject( "Scripting.FileSystemObject" ) ' テンポラリフォルダ TempDir = Fso.GetSpecialFolder(2) on error resume next ' テンポラリフォルダに空の "local.htm" を作成 Set objHandle = Fso.CreateTextFile( TempDir & "\local.htm", True, True ) if Err.Number <> 0 then Exit Function end if objHandle.Close on error goto 0 ' IE を操作するオブジェクト Set IEDocument = WScript.CreateObject( "InternetExplorer.Application" ) ' テンポラリフォルダに作成したファイルを開く IEDocument.Navigate( TempDir & "\local.htm" ) ' 『ファイルを開く』為のコンテンツを作成 IEDocument.document.getElementsByTagName("BODY")(0).innerHTML = "<input id=FilePath type=file>" ' 『ファイルを開く』為に、ボタンをクリックする IEDocument.document.getElementById("FilePath").click ' ファイルを選択していない場合は終了 if IEDocument.document.getElementById("FilePath").value = "" then OpenLocalFileName = "" IEDocument.Quit Set IEDocument = Nothing Exit Function end if ' 選択したファイルのパスを戻す OpenLocalFileName = IEDocument.document.getElementById("FilePath").value ' IE を終了 IEDocument.Quit Set IEDocument = Nothing End Function ' 呼び出し strValue = OpenLocalFileName if strValue = "" then Wscript.Quit end if ' 選択したファイルを表示 Wscript.Echo strValue
昔はセキュリティが甘かったので、ローカルファイルでは無く about:blank で処理できていたのですが、今はインターネットオプションの『サーバーにファイルをアップロードするときにローカル ディレクトリのパスを含める』を有効にしないと、fakepath というパスのみが返るようになっています。それを回避する為にローカルのファイルを使うようにしていますが、その際に管理者権限でないと動作しないので、『管理者として実行を強制』しています ※ UserAccounts.CommonDialog は、XPのみで利用できます BatchHelper オブジェクト をインストールすると以下が使用できます 【 OpenFileName 】 Path = BatchHelper.OpenFileName( [Title], [Filter], [DefaultDir], [DefaultExt], [DefaultName] ) コモンダイアログでファイルのパスを取得します Title (省略可): ダイアログのタイトル Filter (省略可): フィルターリスト ( 例: "CSV,*.csv,全て,*.*" ) DefaultDir (省略可): 初期ディレクトリ DefaultExt (省略可): デフォルトの拡張子 DefaultName (省略可): デフォルトのファイル名 キャンセル時、Path は "" (空文字列) になります ▼ 実行ソースコードサンプル
Set BatchHelper = Wscript.CreateObject("Lbox.BatchHelper") Path = BatchHelper.OpenFileName( ) Wscript.Echo Path
タグ:ファイルを開く