管理者権限で実行する必要がある
昔から一般的に使われて来た方法ですが、ある時から about:blank では、fakepath というパスで返るようになって使えなくなっていました。そこで、ローカルにファイルを作ってそれで対処しています。但し、管理者権限で実行する必要があるので冒頭の処理が必要です。
簡易的な【ファイルを開くダイアログ】
ファイルの種類は設定できません。どうしても VBScript でファイルを開く為のダイアログを正しく使いたい場合は、COM を作成して呼び出す必要があります( exe に実行させて、ファイルで引き渡すと言う手もあります )。しかし、本来 VBScript で行う処理は簡易的なものなのでそこまでこだわる必要も無いのでこれで十分使えると思います。
冒頭の objShell.MinimizeAll は実行しておかないと、エクスプローラから実行した場合その下にファイルを開くダイアログが隠れてしまいます。
このソースは拡張子が .wsf 用です。
<JOB>
<SCRIPT language="VBScript">
' ************************************************
' 管理者として実行を強制する
' ************************************************
Set objShell = Wscript.CreateObject("Shell.Application")
if Wscript.Arguments.Count = 0 then
objShell.ShellExecute "wscript.exe", WScript.ScriptFullName & " runas", "", "runas", 1
Wscript.Quit
end if
' ************************************************
' ファイルを開くダイアログの為に他を全て最小化する
' ************************************************
objShell.MinimizeAll
' ************************************************
' ファイル選択
' ************************************************
strValue = OpenLocalFileName
if strValue = "" then
Wscript.Quit
end if
MsgBox( strValue )
' ************************************************
' InternetExplorer.Application でファイル選択
' ************************************************
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
Set IEDocument = Wscript.CreateObject("InternetExplorer.Application")
IEDocument.Navigate( TempDir & "\local.htm" )
Do While IEDocument.Busy
' 100 ミリ秒
Wscript.Sleep 100
Loop
IEDocument.document.getElementsByTagName("BODY")(0).innerHTML = "<input id='FilePath' type='file'>"
call 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
IEDocument.Quit
Set IEDocument = Nothing
End Function
</SCRIPT>
</JOB>