HTA:尝试将各种输入框和文本区域写入文本文件并复制到剪贴板

HTA: Trying to write various input boxes and text areas into a text file and also copy to clipboard

第一个post这里,很久reader的站点。我搜索了整个 Internet,找不到有效的解决方案,而且我尝试了很多重写,但无法修复它。

我为工作编写 HTA 文件。通常是部门特定的笔记工具,使他们的工作更轻松。除了 copy/paste 之外,我拥有我需要的一切功能。我从我编写的另一个工具中复制了 copy/paste 部分并且它可以工作,但忽略了 \r\n 并且我一直有问题所以我希望用更可靠的东西替换整个部分。所以我想如果我可以只写入一个文本文件,然后将该文本文件复制到剪贴板,那就完美了。因为这也会给他们 "Oops, I didn't mean to clear that text" 选项,只需加载创建的文本文件以查看他们命中的内容。我还计划在清除按钮上实现相同的 "oops",这样它会写入文件,然后清除输入框以保持 oops 文件仍然存在。

快速回顾一下,Write 按钮工作完美,Clear 也可以(但需要写入 oops 文件),而 Copy 按钮需要完全重写。如果可能的话,我想保留所有 VBScript,因为我认为到目前为止我的问题源于两种代码类型和我的 copy/pasting 旧代码到新工具中。

要记住的一件事是我是自学的,只做这些工作。所以不要对我说超级技术,我不会明白的。如果可能的话,请重写所需的部分,让我知道为什么以及代码中发生了什么,这样我就可以掌握这个概念,并能够在将来随着我的学习和成长使用 it/modify 它。

代码如下:

<hta:application
icon="#"
border="thin"
borderStyle="static"
caption="yes"
innerBorder="no"
maximizeButton="no"
minimizeButton="yes"
navigable="no"
scroll="no"
scrollFlat="yes"
singleInstance="yes"
showInTaskbar="yes"
windowState="normal"
contextMenu="no"
version="1.0"
>

<!DOCTYPE html>
<HTML><HEAD><TITLE>Note Tool</TITLE>

<style type='text/css'>
</style>

</head> 

<SCRIPT LANGUAGE="VBScript">
Function Writer_OnClick()

    UserID = (Document.getElementByID("OB1").Value)
    ContactName = (Document.getElementByID("OB2").Value)
    ExternalNotes = (Document.getElementByID("OB3").Value)
    InternalNotes = (Document.getElementByID("OB4").Value)
    CSKBRef = (Document.getElementByID("OB5").Value)

    Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("html01.txt",1)
    HTML01 = objFileToRead.ReadAll()
    objFileToRead.Close
    Set objFileToRead = Nothing

    Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("html02.txt",1)
    HTML02 = objFileToRead.ReadAll()
    objFileToRead.Close
    Set objFileToRead = Nothing

    Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("html03.txt",1)
    HTML03 = objFileToRead.ReadAll()
    objFileToRead.Close
    Set objFileToRead = Nothing

    Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("html04.txt",1)
    HTML04 = objFileToRead.ReadAll()
    objFileToRead.Close
    Set objFileToRead = Nothing

    Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("html05.txt",1)
    HTML05 = objFileToRead.ReadAll()
    objFileToRead.Close
    Set objFileToRead = Nothing

    Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("poop.html",2,true)
    objFileToWrite.WriteLine(HTML01)
    objFileToWrite.WriteLine(UserID)
    objFileToWrite.WriteLine "</li><li>Contacts: "
    objFileToWrite.WriteLine(ContactName)
    objFileToWrite.WriteLine(HTML02)
    objFileToWrite.WriteLine(ExternalNotes)
    objFileToWrite.WriteLine(HTML03)
    objFileToWrite.WriteLine(InternalNotes)
    objFileToWrite.WriteLine(HTML04)
    objFileToWrite.WriteLine(CSKBRef)
    objFileToWrite.WriteLine(HTML05)
    objFileToWrite.Close
    Set objFileToWrite = Nothing
End Function
</SCRIPT>

<script language="VBScript">
    Sub ClearText
       User.Value = "" 
       Contact.Value = "" 
       ExternalNotes.Value = "" 
       InternalNotes.Value = "" 
       CSKB.Value = "" 
    End Sub
</script>

<SCRIPT LANGUAGE="JavaScript">
    function RestoreWindowSize()
    {
    window.resizeTo(750, 420);
    }
</script>

<SCRIPT LANGUAGE="JavaScript">
var desc = new Array();
desc['OB1'] = 'User ID';
desc['OB2'] = 'Contact Name';
desc['OB3'] = 'External Notes';
desc['OB4'] = 'Internal Notes';
desc['OB5'] = 'CSKB Topic';

function CopyFields(){
    var copytext = '';
    for(var i = 0; i < arguments.length; i++){
        copytext += desc[arguments[i]] + ': ' + document.getElementById(arguments[i]).value + '\r\n';
    }
    var tempstore = document.getElementById(arguments[0]).value;
    document.getElementById(arguments[0]).value = copytext;
    document.getElementById(arguments[0]).focus();
    document.getElementById(arguments[0]).select();
    document.execCommand('Copy');
    document.getElementById(arguments[0]).value = tempstore;
}
</script>


<body onload="RestoreWindowSize()">

User ID: <input type="text" name="User" id="OB1"><br>
Contact Name: <input type="text" name="Contact" id="OB2"><br>
External Notes: <textarea rows="5" cols="68" type="text" name="ExternalNotes" id="OB3"></textarea><br>
Internal Notes: <textarea rows="5" cols="68" type="text" name="InternalNotes" id="OB4"></textarea><br>
KB Reference(s): <input type="text" name="CSKB" id="OB5"><br>

<br><br>
<input type="button" onClick="ClearText" value="Clear Me">
<input type="button" name="Copier" value="Copy Me" onClick="CopyFields 'OB1', 'OB2', 'OB3', 'OB4', 'OB5'">
<input type="button" name="Writer" value="Write Me">


</body> 
</html> 

那是很多代码。有什么意义吗。

这是读取剪贴板的方法,写入也是类似的。 HTA 是没有安全性的 IE windows,因此您不需要导航。

Sub Clip
    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = 0
    'Have to navigate to a local file to put IE into Intranet Zone, else this will generate security dialog asking permission
    ie.Navigate2 FilterPath & "Filter.html"
    Do 
        wscript.sleep 100
    Loop until ie.document.readystate = "complete"  
    txt=ie.document.parentwindow.clipboardData.GetData("TEXT")
    ie.quit
    If IsNull(txt) = true then 
        outp.writeline "No text on clipboard"
    else
        outp.writeline txt
    End If
End Sub

来自帮助

clipboardData Object Members


Provides access to predefined clipboard formats for use in editing operations.

Methods

clearData , getData , setData

类似的东西:

<SCRIPT LANGUAGE="VBScript">
Dim desc,ID
desc = Array("User ID","Contact Name","External Notes","Internal Notes","CSKB Topic")
ID = Array("OB1","OB2","OB3","OB4","OB5")
Function CopyFields()
Dim copytext,i
    copytext = ""
    For i = LBound(desc) to UBound(desc)
        copytext = copytext & desc(i) & " : " & document.getElementById(ID(i)).value & vbcrlf
    Next
    Msgbox copytext
    document.parentwindow.clipboardData.SetData "text",copytext
End Function
</script>

并将 HTML 部分更改为:

<input type="button" name="Copier" value="Copy Me" onClick="CopyFields()">