HTA 中的另存为...对话框

Save As... dialog box in HTA

我有一个 HTA 文件,我想制作一个保存文件的功能。 HTML、JavaScript 或 VBScript 中是否有任何方法可以打开“另存为...”对话框,以便用户可以选择要保存文件的位置和名称?

假设您能够完成打开和复制部分,这里有一个(恐怕已经很久没有尝试过的)例程演示了如何在 VBScript 中使用保存为普通对话,它应该在 HTA 中工作。

Sub SaveAs
    Dim oDLG
    Set oDLG=CreateObject("MSComDlg.CommonDialog") 
    With oDLG
        .DialogTitle="SaveAs"
        .Filter="Scripts|*.vbs;*.hta;*.wsf;*.js|Text Files|*.txt|All files|*.*"
        .MaxFileSize=255
        .ShowSave
        If .FileName<>"" Then
            FileName=.FileName
            Save
        End If
    End With
    Set oDLG=Nothing
    DisplayTitle
End Sub

但是请注意来自 How can I use the common Save As dialog from VBScript? 的评论表明您可能需要将许可证安装到注册表中或查找并安装 Visual Studio 或 HTML 帮助。 VS 现在有许多免费版本,所以这个问题不像以前那么严重了。

下面是一个 HTA 文件的例子,展示了如何使用 JS 和 VBS 打开选择文件对话框 "sandbox":

<html>
    <head>
        <script language="javascript">
            function OpenDialogJs() {
                output.innerText = d.object.openFileDlg(i.value, null, f.value, t.value);
            }
        </script>
        <script language="vbscript">
            Sub OpenDialogVbs()
                output.innerText = d.object.openFileDlg(i.value, , f.value, t.value)
            End Sub
        </script>
    </head>
    <body>
        <object id="d" classid="clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b" width=0 height=0></object>
        Initial dir <input id="i" type="text" value="C:\*"></input><br>
        Filter <input id="f" type="text" value="All files (*.*)|*.*|Microsoft Word (*.doc;*.docx)|*.doc;*.docx"></input><br>
        Title <input id="t" type="text" value="Save As..."></input><br>
        <input type="button" value="Open / js" onclick="OpenDialogJs()"></input>
        <input type="button" value="Open / vbs" onclick="OpenDialogVbs()"></input>
        <div id="output"></div>
    </body>
</html>

这是在 hta 中制作自己的“另存为”对话框的方法:

var folder = new ActiveXObject("WScript.Shell").SpecialFolders("mydocuments");    //The default folder in which the file is saved, in this case My Documents
function saveAs(ext){    //ext is the file extension without a dot, for exampe: html, NOT .html
    var s = window.showModalDialog("saveas.hta?" + ext, window, "dialogWidth:609px; dialogHeight:386px");
    return s;
}

saveas.hta:

<html>
<head>
    <title>Save as</title>
    <meta http-equiv="MSThemeCompatible" content="yes" />
    <style type="text/css">
        body, td {
            margin: 8px;
            font-family: tahoma;
            font-size: 10pt;
        }

        button.normal {
            position: absolute;
            left: 11px;
            border: none;
            padding: 2px;
            background: none;
            width: 100%;
            font-size: 8pt;
        }

        button.hover {
            position: absolute;
            left: 11px;
            width: 100%;
            font-size: 8pt;
        }
    </style>
    <script type="text/javascript">
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var ws = new ActiveXObject("WScript.Shell");
        var span = new Array();

        window.onload = function(){
                document.body.getElementsByTagName("select")[0].innerHTML = "<option value='." + location.href.split("?")[1] + "' selected>" + location.href.split("?")[1] + " file (*." + location.href.split("?")[1] + ")</option>"
                if(!fso.FolderExists(window.dialogArguments.folder)){window.dialogArguments.folder = window.dialogArguments.defaultFolder}
                path.value = window.dialogArguments.folder;
                lit_fold(path.value);
                path.blur();
                namn.focus();
        }

        function spara(){
                if(!new ActiveXObject("Scripting.FileSystemObject").FileExists(((path.value == "file://") ? ws.SpecialFolders("Appdata") + "\Microsoft\Windows\Network Shortcuts" : path.value) + "\" + namn.value.replace(/\"/gi,"") + ((namn.value.indexOf("\"") == -1) ? type.getElementsByTagName("option")[type.selectedIndex].value : "")) || confirmYesNo("The file " + ((path.value == "file://") ? ws.SpecialFolders("Appdata") + "\Microsoft\Windows\Network Shortcuts" : path.value) + "\" + namn.value.replace(/\"/gi,"") + ((namn.value.indexOf("\"") == -1) ? type.getElementsByTagName("option")[type.selectedIndex].value : "") + " already exists.\nDo you want to replace it?")){
                        var retourne = new Object();
                        window.returnValue = ((path.value == "file://") ? ws.SpecialFolders("Appdata") + "\Microsoft\Windows\Network Shortcuts" : path.value) + "\" + namn.value.replace(/\"/gi,"") + ((namn.value.indexOf("\"") == -1) ? type.getElementsByTagName("option")[type.selectedIndex].value : "");
                        window.close();
                }
        }
        function kolla(){
                if(new ActiveXObject("Scripting.FileSystemObject").FolderExists(path.value) || path.value == "file://"){
                        window.dialogArguments.folder = path.value;
                        lit_fold(path.value);
                }
                else{
                        alert("Could not find this folder");
                        path.value = window.dialogArguments.folder;
                }
        }

        function lit_fold(chemin){
                if(chemin == "file://"){
                        ok.disabled = !fso.FolderExists(ws.SpecialFolders("Appdata") + "\Microsoft\Windows\Network Shortcuts");
                        list.innerHTML = "";
                        span = new Array();
                        var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
                        for(i = 0; i < 26; i++){
                                if(fso.DriveExists(alphabet[i] + ":\")){
                                        var l = span.length;
                                        span[l] = document.createElement("span");
                                        span[l].onclick = function(){
                                                var wse = window.event.srcElement;
                                                if(wse.tagName == "IMG"){wse = window.event.srcElement.parentElement}
                                                for(i = 0; i < span.length; i++){
                                                        span[i].style.background = "none";
                                                        span[i].style.color = "black";
                                                }
                                                wse.style.background = "rgb(1,153,255)";
                                                wse.style.color = "white";
                                        }
                                        span[l].ondblclick = function(){
                                                path.value = window.event.srcElement.value;
                                                kolla();
                                                lit_fold(path.value);
                                        }
                                        span[l].style.width = "100%";
                                        span[l].innerHTML = "<img src='drive" + (fso.GetDriveName(fso.GetSpecialFolder(0)) == alphabet[i] + ":" ? fso.GetDrive(alphabet[i] + ":").DriveType + "bis" : fso.GetDrive(alphabet[i] + ":").DriveType) + ".png' width='16' height='16' value='" + alphabet[i] + ":\'/>&nbsp;" + fso.GetDrive(alphabet[i] + ":").VolumeName + " (" + alphabet[i] + ":)<br/>";
                                        span[l].value = alphabet[i] + ":\";
                                        list.appendChild(span[l]);
                                }
                        }
                        if(fso.FolderExists(ws.SpecialFolders("Appdata") + "\Microsoft\Windows\Network Shortcuts")){
                                chemin = ws.SpecialFolders("Appdata") + "\Microsoft\Windows\Network Shortcuts";
                                var ff = new Enumerator(fso.GetFolder(chemin).subfolders);
                                for(; !ff.atEnd(); ff.moveNext()){
                                        var l = span.length;
                                        span[l] = document.createElement("span");
                                        span[l].onclick = function(){
                                                var wse = window.event.srcElement;
                                                if(wse.tagName == "IMG"){wse = window.event.srcElement.parentElement}
                                                for(i = 0; i < span.length; i++){
                                                        span[i].style.background = "none";
                                                        span[i].style.color = "black";
                                                }
                                                wse.style.background = "rgb(1,153,255)";
                                                wse.style.color = "white";
                                        }
                                        span[l].ondblclick = function(){
                                                path.value = window.event.srcElement.value;
                                                kolla();
                                                lit_fold(path.value);
                                        }
                                        span[l].style.width = "100%";
                                        span[l].innerHTML = "<img src='folder.png' width='16' height='16' value='" + ff.item() + "'/>&nbsp;" + fso.GetFolder(ff.item()).Name + "<br/>";
                                        span[l].value = new String(ff.item());
                                        list.appendChild(span[l]);
                                }
                                ff = new Enumerator(fso.GetFolder(chemin).files);
                                for(; !ff.atEnd(); ff.moveNext()){
                                        if(fso.getExtensionName(ff.item()) == location.href.split("?")[1]){
                                                var l = span.length;
                                                span[l] = document.createElement("span");
                                                span[l].onclick = function(){
                                                        var wse = window.event.srcElement;
                                                        if(wse.tagName == "IMG"){wse = window.event.srcElement.parentElement}
                                                        for(i = 0; i < span.length; i++){
                                                                span[i].style.background = "none";
                                                                span[i].style.color = "black";
                                                        }
                                                        wse.style.background = "rgb(1,153,255)";
                                                        wse.style.color = "white";
                                                        namn.value = wse.value.slice(0,-4);
                                                }
                                                span[l].ondblclick = spara;
                                                span[l].style.width = "100%";
                                                span[l].innerHTML = "<img src='file.png' width='16' height='16'/>&nbsp;" + fso.getBaseName(ff.item()) + "<br/>";
                                                span[l].value = new String(ff.item() + " ").slice(chemin.length,-1).replace("\","");
                                                list.appendChild(span[l]);
                                        }
                                }
                        }
                }
                else{
                        ok.disabled = false;
                        list.innerHTML = "";
                        span = new Array();
                        var ff = new Enumerator(fso.GetFolder(chemin).subfolders);
                        for(; !ff.atEnd(); ff.moveNext()){
                                var l = span.length;
                                span[l] = document.createElement("span");
                                span[l].onclick = function(){
                                        var wse = window.event.srcElement;
                                        if(wse.tagName == "IMG"){wse = window.event.srcElement.parentElement}
                                        for(i = 0; i < span.length; i++){
                                                span[i].style.background = "none";
                                                span[i].style.color = "black";
                                        }
                                        wse.style.background = "rgb(1,153,255)";
                                        wse.style.color = "white";
                                }
                                span[l].ondblclick = function(){
                                        path.value = window.event.srcElement.value;
                                        kolla();
                                        lit_fold(path.value);
                                }
                                span[l].style.width = "100%";
                                span[l].innerHTML = "<img src='folder.png' width='16' height='16' value='" + ff.item() + "'/>&nbsp;" + fso.GetFolder(ff.item()).Name + "<br/>";
                                span[l].value = new String(ff.item());
                                list.appendChild(span[l]);
                        }
                        ff = new Enumerator(fso.GetFolder(chemin).files);
                        for(; !ff.atEnd(); ff.moveNext()){
                                if(fso.getExtensionName(ff.item()) == location.href.split("?")[1]){
                                        var l = span.length;
                                        span[l] = document.createElement("span");
                                        span[l].onclick = function(){
                                                var wse = window.event.srcElement;
                                                if(wse.tagName == "IMG"){wse = window.event.srcElement.parentElement}
                                                for(i = 0; i < span.length; i++){
                                                        span[i].style.background = "none";
                                                        span[i].style.color = "black";
                                                }
                                                wse.style.background = "rgb(1,153,255)";
                                                wse.style.color = "white";
                                                namn.value = wse.value.slice(0,-4);
                                        }
                                        span[l].ondblclick = spara;
                                        span[l].style.width = "100%";
                                        span[l].innerHTML = "<img src='file.png' width='16' height='16'/>&nbsp;" + fso.getBaseName(ff.item()) + "<br/>";
                                        span[l].value = new String(ff.item() + " ").slice(chemin.length,-1).replace("\","");
                                        list.appendChild(span[l]);
                                }
                        }
                }
        }

        document.onkeydown = function(){
                if(window.event.keyCode == 13){
                        spara();
                }
                if(window.event.keyCode == 27){
                        window.close();
                }
                if(window.event.keyCode == 116){
                        lit_fold(path.value);
                }
        }
    </script>
</head>
<body bgcolor="buttonface" ondragstart="return false">
    <table width="100%">
        <tr>
            <td width="15%">Folder:</td>
            <td width="85%"><input type="text" id="path" onchange="kolla()" style="width:100%" />
        </tr>
        <tr height="258px">
            <td valign="top">
                <button class="normal" onmouseover="window.event.srcElement.className = 'hover'" onmouseout="window.event.srcElement.className = 'normal'" onclick="path.value = ws.SpecialFolders('Desktop'); kolla()" title="Desktop" style="top:40px"><img src="desktop.png" width="32" height="32" onmouseover="document.body.getElementsByTagName('button')[0].className='hover'" onmouseout="document.body.getElementsByTagName('button')[0].className='normal'" /><br />Dektop</button><br />
                <button class="normal" onmouseover="window.event.srcElement.className = 'hover'" onmouseout="window.event.srcElement.className = 'normal'" onclick="path.value = ws.SpecialFolders('MyDocuments'); kolla()" title="My documents" style="top:90px"><img src="mydocs.png" width="32" height="32" onmouseover="document.body.getElementsByTagName('button')[1].className='hover'" onmouseout="document.body.getElementsByTagName('button')[1].className='normal'" /><br />My documents</button><br />
                <button class="normal" onmouseover="window.event.srcElement.className = 'hover'" onmouseout="window.event.srcElement.className = 'normal'" onclick="path.value = 'file://'; kolla()" title="Computer" style="top:140px"><img src="computer.png" width="32" height="32" onmouseover="document.body.getElementsByTagName('button')[2].className='hover'" onmouseout="document.body.getElementsByTagName('button')[2].className='normal'" /><br />Computer</button>
            </td>
            <td><div id="list" style="width:100%; height:100%; background:white; overflow:scroll; padding:4px"></div></td>
        </tr>
        <tr>
            <td>File name:</td>
            <td><input type="text" id="namn" style="width:100%" onfocus="for(i = 0; i < span.length; i++){span[i].style.background = 'none'; span[i].style.color = 'black'}" /></td>
        </tr>
        <tr>
            <td>Type:</td>
            <td>
                <select id="type" style="width:100%"></select>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td style="text-align:right">
                <button id="ok" onclick="spara()" style="width:94px">Save</button>&nbsp;&nbsp;&nbsp;
                <button onclick="window.close()" style="width:86px">Cancel</button>
            </td>
        </tr>
    </table>
</body>
</html>

您可以制作自己的图标或下载这些图标:

computer.png:

desktop.png:

drive0.png:

drive1.png:

drive2.png:

drive2bis.png:

drive3.png:

drive4.png:

drive5.png:

file.png:

folder.png:

mydocs.png:

您可以使用 Microsoft Excel Application 对象的 GetSaveAsFileName 方法打开另存为...对话框。
下面的示例显示了如何执行此操作:

<html>
    <head>
        <title>Create text file</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="author" content="Javad">
        <script language="javascript">
            var excel = new ActiveXObject("Excel.Application");
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var shell = new ActiveXObject("Shell.Application");

            function createTextFile(content)
            {
                event.returnValue = false;
                shell.MinimizeAll();
                var path = excel.GetSaveAsFileName(fso.GetAbsolutePathName("My text file"), "Text files (*.txt;*.log),*.txt;*.log,All files (*.*),*.*", 1, "Save As...");
                window.focus();

                if (!path) return;

                var ts = fso.OpenTextFile(path,2,true);
                ts.WriteLine(content);
                ts.Close();
            }
        </script>
        <HTA:Application windowstate="maximize">
    </head>
    <body>
        <form onsubmit="createTextFile(this.cnt.value)">
            Content of the text file: <input type="text" name="cnt" value="Hello."><br>
            <button type="submit">Create text file</button>
        </form>
    </body>
</html>

在上面的示例中,表单中有一个文本字段和一个提交按钮。 用户应在文本字段中键入内容并单击按钮。 当用户单击该按钮时,该程序会打开另存为...对话框并让用户选择 he/she 想要保存文本文件的位置。
然后这个程序在指定的位置创建一个文本文件,并将该字段的值写入文本文件中。

注意:打开“另存为...”对话框时,它不会获得焦点。我不知道为什么。也许这是 Excel 2007 的错误之一。所以代码 shell.MinimizeAll() 写在第 14 行,以便从所有 windows 中删除焦点,所以另存为...对话框可以接收焦点。 正如我所说,在 Excel 2007 中存在这个问题。也许这个错误在 Excel.

的更高版本中得到修复