2022年5月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
無料ブログはココログ

« VBAで、テキストファイルの文字コードを自動判定します。 | トップページ | HTAでもwindow.close()は復帰します! »

2007年2月21日 (水)

Windows2000+WSH5.6で使える「ファイルを開く」ダイアログ

WindowsXPなら、いろいろ使えるのですが、Windows2000だと、困ります。
HtmlDlgHelperにopenfiledlg()がありますが、HTMLに組み込まないと使えないみたい。
そこで、これをHTAに動的に組み込んで使います。

MsgBox OpenFileDlg("C:\Program Files\*.txt","テキスト (*.txt)|*.txt|すべてのファイル (*.*)|*.*|","ファイルの選択")

Function OpenFileDlg(Path,Filter,Title)
Dim oExec
Set oExec=CreateObject("WScript.Shell").Exec("MSHTA.EXE ""javascript:new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0).ReadAll()""")
oExec.StdIn.WriteLine "<object id=HtmlDlgHelper classid=CLSID:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object>"
oExec.StdIn.WriteLine "<script language=vbscript>"
oExec.StdIn.WriteLine "resizeTo 0,0"
oExec.StdIn.WriteLine "Sub window_onload()"
oExec.StdIn.WriteLine "CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write HtmlDlgHelper.object.openfiledlg(""" & Path & """,,""" & Filter & """,""" & Title & """)"
oExec.StdIn.WriteLine "close"
oExec.StdIn.WriteLine "End Sub"
oExec.StdIn.WriteLine "</script>"
oExec.StdIn.WriteLine "<hta:application caption=no showintaskbar=no />"
oExec.StdIn.Close
OpenFileDlg=oExec.StdOut.ReadAll()
If InStr(OpenFileDlg,vbNullChar) Then OpenFileDlg=Left(OpenFileDlg,InStr(OpenFileDlg,vbNullChar)-1) '注意!
End Function

注意!
openfiledlg()は、Null Terminated String、所謂'SZ'で返ってくる変な仕様なので、切り捨てます。

« VBAで、テキストファイルの文字コードを自動判定します。 | トップページ | HTAでもwindow.close()は復帰します! »