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

Posted

技术标签:

【中文标题】HTA 中的另存为...对话框【英文标题】:Save As... dialog box in HTA 【发布时间】:2015-05-23 12:08:38 【问题描述】:

我有一个 HTA 文件,我想创建一个保存文件的函数。 htmljavascript 或 VBScript 中是否有任何方法可以打开“另存为...”对话框,以便用户可以选择保存文件的位置和名称?

【问题讨论】:

可能有帮助:***.com/questions/21559775/… 和 ***.com/questions/4386124/…,尤其是 ***.com/a/4464323/18771 【参考方案1】:

您可以使用 Microsoft Excel 应用程序对象的 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>

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

注意:当另存为...对话框打开时,它不会获得焦点。我不知道为什么。可能是 Excel 2007 的 bug 之一。所以代码 shell.MinimizeAll() 写在第 14 行,目的是从所有窗口中移除焦点,以便“另存为...”对话框可以接收焦点。 正如我所说,Excel 2007 中存在此问题。也许此错误已在更高版本的 Excel 中修复。

【讨论】:

"也许这个错误在更高版本的 Excel 中已修复。" 不,不是,我有 Excel 2016,但仍然存在相同的错误。无论如何,除此之外,它完美无缺,感谢您的回答。【参考方案2】:

下面是一个 HTA 文件示例,展示了如何使用 JS 和 VBS 以及小“沙盒”打开选择文件对话框:

<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>

【讨论】:

这不会保存文件,它会打开一个现有文件。【参考方案3】:

假设您能够执行打开和复制部分,这里有一个(恐怕在很长一段时间内都没有尝试过)例程演示如何使用保存作为 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? 的 cmets,它们表明您可能需要将许可证安装到注册表中,或者查找并安装 Visual Studio 或 HTML 帮助。 VS 现在有许多免费版本,因此这已不再是过去的问题了。

【讨论】:

这不起作用(有关详细信息,请参阅here)。还有其他不使用 MSComDlg.CommonDialog 的解决方案吗? 当我在我的 Windows 8.1 平板电脑上试用它时,它确实有效。诀窍是满足我认为的先决条件。我安装了 Visual Studio,所以这可能就是它对我有用的原因。【参考方案4】:

这是一种在 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 >
        <tr>
            <td >Folder:</td>
            <td ><input type="text" id="path" onchange="kolla()" style="width:100%" />
        </tr>
        <tr >
            <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"   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"   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"   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>

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

计算机.png:

桌面.png:

驱动器0.png:

驱动器1.png:

驱动器2.png:

drive2bis.png:

drive3.png:

drive4.png:

驱动器5.png:

文件.png:

文件夹.png:

mydocs.png:

【讨论】:

以上是关于HTA 中的另存为...对话框的主要内容,如果未能解决你的问题,请参考以下文章

我想弹出我自己的对话框(将文件保存在服务器上而不询问目标路径位置)而不是 adobe 的另存为对话框

另存为Dailog,不会弹出

文件相关操作 - 另存为

按钮显示另存为对话框,然后保存到设置的位置

如何在 Windows 上使用 GetSaveFileName 检测“另存为类型:”组合框何时更改?

程序中的对话框应用- ”另存为“对话框