2

I am trying to extract a few strings into Windows clipboard, which is working fine; but attempting to put a carriage return and line feed between each string is causing problems, I understand it is related to this thread: Escape angle brackets in a Windows command prompt

But I cannot get my code to work with the provided info.

This is my code without any vbCRLF;

Dim objShell
Set objShell = CreateObject("WScript.shell")

objShell.Run "cmd /C echo " & Control_Address1.text & Control_Address2.text & Control_Address3.text & Control_Address4.text & "  | CLIP", 2

Which results in the contents on the strings Address1-4 being output one after each other but I want them in this format.

Address1
Address2
Address3
Address4

I tried:

objShell.Run "cmd /C echo " & Control_Address1.text & vbCRLF & Control_Address2.text & vbCRLF & Control_Address3.text & vbCRLF & Control_Address4.text & "  | CLIP", 2

Which resulted in 'vbCTRL' being put in the clipboard and I learned about escape characters as a result. I then attempted the following code based upon the above thread.

objShell.Run "cmd /C echo " & Control_Address1.text ^& vbCRLF ^& Control_Address2.text ^& vbCRLF ^& Control_Address3.text & vbCRLF & Control_Address4.text & "  | CLIP", 2

But this results in syntax errors, I'm heading in the right direction I think but appear to be stuck, any help you could offer a newbie would be greatly appreciated!

Community
  • 1
  • 1
Peter
  • 55
  • 7
  • Thanks for the format correction MC ND, I'm not sure why it didn't post the the correct layout. – Peter Sep 25 '14 at 11:05

1 Answers1

3

The problem is that you can not place a CRLF inside a command line. But, if the data you need to store in the clipboard is less than 8191 characters, you can store it inside a environment variable and, with delayed expansion, retrieve it from cmd.

Option Explicit

Dim buffer
    buffer = Join( Array("Address1", "Address2", "Address3"), vbCRLF )

    With WScript.CreateObject("WScript.Shell") 
        .Environment("Process").Item("##") = buffer
        .Run "cmd /c ""(cmd /v:on /c echo(!##!)|clip"" ", 0, true
    End With

The data to place into the clipboard is stored in a environment variable (called ##), then the command processor is invoked with delayed expansion enabled (/v:on), a requirement to be able to properly echo the content of the variable with the CRLF.

There is also an alternative method, using Internet Explorer

....
With CreateObject("InternetExplorer.Application")
    .Navigate "about:blank"
    .document.parentwindow.clipboardData.SetData "text", buffer
    .Quit
End With

But to get this working, you will probably need to adjust the rights in IE. By default, a dialog is shown asking the user to grant the script access to the clipboard.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thank you for the explanation of why that doesn't work. I have just tried the first piece of code (no browser is involved in what I'm doing) I get the following error: 'Error in control script: Expected statement (Microsoft VBScript compalation error): Option Explicit' I've not used Explicit Statements before, after a brief Googling I tried a switch of On or Off, neither worked, the error remained the same. – Peter Sep 25 '14 at 12:09
  • @Peter, `Option Explicit` is a directive that forces to declare all variables used in code (a good practice). `On` and `Off` are optional, and `On` is the default value. But, `Option Explicit` must be placed before any other instruction. So, change placement or try to execute without it. – MC ND Sep 25 '14 at 12:19
  • @Peter, and the second code does implies your project was using IE. It just uses the automation capabilities of IE to access the `clipboardData` that it exposes – MC ND Sep 25 '14 at 12:22
  • My apologies, I will bear in mind to be more specific in future. After a little fighting I got the following code to work: Dim buffer buffer = Join( Array(Control_Address1.text, Control_Address2.text, Control_Address3.text, Control_Address4.text), vbCRLF ) With CreateObject("WScript.Shell") .Environment("Process").Item("##") = buffer .Run "cmd /c ""(cmd /v:on /c echo(!##!)|clip"" ", 0, true End With Now to work out how to make it conditional.. Thank you for all your time and help MC ND, you've been great! – Peter Sep 25 '14 at 12:37
  • My apologies, I will bear in mind to be more specific in future. After a little fighting I got the following code to work: Dim buffer buffer = Join( Array(Control_Address1.text, {}Control_Address2.text, Control_Address3.text, {}Control_Address4.text), vbCRLF ) {}With CreateObject("WScript.Shell") {}.Environment("Process").Item("##") = buffer {}.Run "cmd /c ""(cmd /v:on /c echo(!##!)|clip"" ", 0, {}true End With Now to work out how to make it conditional.. Thank you for all your time and help MC ND, you've been great! – Peter Sep 25 '14 at 13:44