我在寻找一个方法把一些文字上的剪贴板 VBScript.VBScript在的问题将部署的一部分,我们登录脚本。我想避免使用任何不可以在一个清洁Windows XP系统。

编辑: 在回答有关问题这是什么。

我们希望鼓励用户在我们的组织使用的文件服务器传输文件,而不是不断地发送附件通过电子邮件。的最大障碍之一是,它并不总是显而易见的人什么是正确的网络路径是一个文件夹。我们开发了一个快速脚本,并连接到窗户上下文的菜单,以便用户能够正确的点击任何文件,并获得一个网址,他们可以通过电子邮件为有人在我们的组织。

我想URL显示在对话框也可以放在剪贴板。

GetNetworkPath

有帮助吗?

解决方案

Microsoft没有为VBScript提供直接访问剪贴板的方法。如果您在'clipboard' /keydifferencesbetweenvisualbasicforapplicationsvbscript.asp“rel =”nofollow noreferrer“>这个网站你会看到:

  

虽然Visual Basic for Applications支持Screen,Printer,App,Debug,Err和Clipboard对象,但VBScript仅支持Err对象。因此,VBScript不允许您访问鼠标指针或剪贴板等有用的对象。但是,您可以使用Err对象为应用程序提供运行时错误处理。

因此间接使用记事本可能只是你能用VBScript做的最好的。

其他提示

我发现的另一个解决方案在我看来并不完美,但没有恼人的安全警告是使用来自w2k3服务器的clip.exe。

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE

http://www.petri.co.il/quickly_copy_error_and_display_messages.htm

根据下面的问题使用多行字符串的示例

Dim string
String = "text here" &chr(13)& "more text here"
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo " & String & " | clip", 0, TRUE

我到目前为止找到的最接近的解决方案是使用IE来获取和设置剪贴板上的东西的方法。此解决方案的问题是用户获得安全警告。我很想把'about:blank'移到本地计算机安全区,所以我没有得到警告,但我不确定这会带来什么样的安全隐患。

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
objIE.document.parentwindow.clipboardData.SetData "text", "Hello This Is A Test"
objIE.Quit

http://www.microsoft.com/technet/脚本中心/资源/ qanda / dec04 / hey1215.mspx

使用Microsoft的clip.exe最接近拥有干净的Windows XP系统解决方案。但是,您不必调用CMD.EXE来托管它以便使用它。您可以直接调用它并在脚本代码中写入其输入流。关闭输入流后,clip.exe会将内容直接写入剪贴板。

Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("clip")
Set oIn = oExec.stdIn
oIn.WriteLine "Something One"
oIn.WriteLine "Something Two"
oIn.WriteLine "Something Three"
oIn.Close

如果您需要等待剪辑完成,然后脚本才能继续处理,请添加

' loop until we're finished working.
Do While oExec.Status = 0
    WScript.Sleep 100
Loop

不要忘记发布你的对象

Set oIn = Nothing
Set oExec = Nothing

为了避免安全警告相关的互联网浏览器和剪贴板访问,我建议你用这个词用对象及其方法把你的数据上的剪贴板。当然你可以仅仅利用这一机MS Word安装,但是这些天来,大多数。(*尽管事实上,你要的东西在一个"干净"系统:)*)

' Set what you want to put in the clipboard '
strMessage = "Imagine that, it works!"

' Declare an object for the word application '
Set objWord = CreateObject("Word.Application")

' Using the object '
With objWord
   .Visible = False         ' Don't show word '
   .Documents.Add           ' Create a document '
   .Selection.TypeText strMessage   ' Put text into it '
   .Selection.WholeStory        ' Select everything in the doc '
   .Selection.Copy          ' Copy contents to clipboard '
   .Quit False          ' Close Word, don't save ' 
End With

你可以找到细节上的MS Word应用程序的对象,其方法在这里: http://msdn.microsoft.com/en-us/library/aa221371(办公室。11条)。aspx

这是使用“剪辑”的另一个版本。命令,它避免在字符串末尾添加回车符,换行符:

strA= "some character string"

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /C echo . | set /p x=" & strA & "| c:\clip.exe", 2

s = "String: """ & strA & """ is on the clipboard."
Wscript.Echo s

我只在XP中测试过这个。 clip.exe是从 http://www.petri.co.il/downloads/clip下载的.zip 并放在C:\。

没有安全警告,完全放过并获取访问权限:

'create a clipboard thing
 Dim ClipBoard
 Set Clipboard = New cClipBoard

 ClipBoard.Clear  
 ClipBoard.Data = "Test"

Class cClipBoard
        Private objHTML

                Private Sub Class_Initialize
                        Set objHTML = CreateObject("htmlfile")
                End Sub

                Public Sub Clear()
                        objHTML.ParentWindow.ClipboardData.ClearData()
                End Sub

                Public Property Let Data(Value)
                        objHTML.ParentWindow.ClipboardData.SetData "Text" , Value
                End Property

                Public Property Get Data()
                        Data = objHTML.ParentWindow.ClipboardData.GetData("Text")
                End Property

                Private Sub Class_Terminate
                        Set objHTML = Nothing
                End Sub

End Class

使用示例。

' Create scripting object
Dim WShell, lRunUninstall
Set WShell = CreateObject("WScript.Shell")
WShell.sendkeys "^c"
WScript.Sleep 250
bWindowFound = WShell.AppActivate("Microsoft Excel")
 WShell.sendkeys ClipBoard.Data

我已经找到了一种方法来复制多线信息的剪贴板vbscript/记录。

序列:

  • 与JS生成最终的"formatted string"你需要复制到剪贴板
  • 生(txt)文件的"formatted string"
  • 使用类型命令从cmd贴的信息夹通过管道

例脚本:

Function CopyToClipboard( sInputString )

    Dim oShell: Set oShell = CreateObject("WScript.Shell")
    Dim sTempFolder: sTempFolder = oShell.ExpandEnvironmentStrings("%TEMP%")
    Dim sFullFilePath: sFullFilePath = sTempFolder & "\" & "temp_file.txt"

    Const iForWriting = 2, bCreateFile = True
    Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    With oFSO.OpenTextFile(sFullFilePath, iForWriting, bCreateFile)
        .Write sInputString
        .Close
    End With

    Const iHideWindow = 0, bWaitOnReturnTrue = True
    Dim sCommand: sCommand = "CMD /C TYPE " & sFullFilePath & "|CLIP"
    oShell.Run sCommand, iHideWindow, bWaitOnReturnTrue

    Set oShell = Nothing
    Set oFSO = Nothing

End Function

Sub Main

    Call CopyToClipboard( "Text1" & vbNewLine & "Text2" )

End Sub

Call Main

我设计了另一种使用IE的方法,但却避免了安全警告......

顺便说一句..这个函数是用JavaScript编写的..但你可以轻松地将它转换为VBScript ..

function CopyText(sTxt) {
    var oIe = WScript.CreateObject('InternetExplorer.Application');
    oIe.silent = true;
    oIe.Navigate('about:blank');
    while(oIe.ReadyState!=4) WScript.Sleep(20);
    while(oIe.document.readyState!='complete') WSript.Sleep(20);
    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>";
    var oTb = oIe.document.getElementById('txtArea');
    oTb.value = sTxt;
    oTb.select();
    oTb = null;
    oIe.ExecWB(12,0);
    oIe.Quit();
    oIe = null;
}

这是Srikanth的方法翻译成vbs

function SetClipBoard(sTxt)
    Set oIe = WScript.CreateObject("InternetExplorer.Application")
    oIe.silent = true
    oIe.Navigate("about:blank")
    do while oIe.ReadyState <> 4
        WScript.Sleep 20
    loop

    do while oIe.document.readyState <> "complete"
        WScript.Sleep 20
    loop

    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>"
    set oTb = oIe.document.getElementById("txtArea")
    oTb.value = sTxt
    oTb.select
    set oTb = nothing
    oIe.ExecWB 12,0
    oIe.Quit
    Set oIe = nothing
End function


function GetClipBoard()
    set oIe = WScript.CreateObject("InternetExplorer.Application")
    oIe.silent = true
    oIe.Navigate("about:blank")
    do while oIe.ReadyState <> 4
        WScript.Sleep 20
    loop

    do while oIe.document.readyState <> "complete"
        WScript.Sleep 20
    loop 

    oIe.document.body.innerHTML = "<textarea id=txtArea wrap=off></textarea>"
    set oTb = oIe.document.getElementById("txtArea")
    oTb.focus   
    oIe.ExecWB 13,0
    GetClipBoard = oTb.value
    oTb.select
    set oTb = nothing
    oIe.Quit
    Set oIe = nothing
End function

最简单的方法是使用内置的 mshta.exe 功能:

sText = "Text Content"
CreateObject("WScript.Shell").Run "mshta.exe ""javascript:clipboardData.setData('text','" & Replace(Replace(sText, "\", "\\"), "'", "\'") & "');close();""", 0, True

要将包含双引号char &quot; 的字符串放入剪贴板,请使用以下代码:

sText = "Text Content and double quote "" char"
CreateObject("WScript.Shell").Run "mshta.exe ""javascript:clipboardData.setData('text','" & Replace(Replace(Replace(sText, "\", "\\"), """", """"""), "'", "\'") & "'.replace('""""',String.fromCharCode(34)));close();""", 0, True

请参阅这篇文章。它描述了从剪贴板中读取的hacky方法,但我想它也可以适应也写入剪贴板,例如将Ctrl + V更改为Ctrl + A然后按Ctrl + C.

在Class ClipBoard 中,Clear sub和Let Data子都不起作用。我的意思是它们对Windows Clipboard 没有影响。实际上,具有讽刺意味的是,唯一可行的是您未包含在您的示例中的子,即获取数据! (我已经多次测试过这段代码了。)

然而,这不是你的错。我试图用ClipboardData.SetData将数据复制到剪贴板,这是不可能的。至少不是通过创建“htmlfile”。宾语。也许它通过创建“InternetExplorer.Application”的实例来工作。正如我在少数情况下看到的那样,但我没有尝试过。我讨厌为这么简单的任务创建应用程序实例!

Alkis

如果它只是文本不能简单地创建一个文本文件并在需要时读入内容?

使用 SendKeys()方法的另一种选择,显然是kludge。

没有安全警告,行末没有回车

' value to put in Clipboard
mavaleur = "YEAH"

' current Dir
path = WScript.ScriptFullName
GetPath = Left(path, InStrRev(path, "\"))

' Put the value in a file
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile=GetPath & "fichier.valeur"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write mavaleur
objFile.Close

' Put the file in the Clipboard
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c clip < " & outFile, 0, TRUE

' Erase the file
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile outFile
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top