Copy to Clipboard Questions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Question 1:
If I have text in cell A1 and I use;
Range("A1").Copy
the text in A1 followed by an end-of-line (0Dh 0Ah) is copied to the
clipboard. How can I copy the text without the end-of-line?

Question 2:
I have a variable of type string. Is there any way to copy that to the
clipboard without having to place it in a cell first. The following code
does not work but should indicate what I am trying to do:
Dim s as String
s = "ABC"
s.Copy

Many thanks.
 
Question 1:
If I have text in cell A1 and I use;
Range("A1").Copy
the text in A1 followed by an end-of-line (0Dh 0Ah) is copied to the
clipboard. How can I copy the text without the end-of-line?

I think if you use .Copy, you're going to get the line terminator no
matter what. You could strip the line terminator from the end of the string.

Another option is to get the text from A1 directly.

txt = Range("A1").Value

txt won't contain the line terminator. Then see below if you need to put
txt on the clipboard.
Question 2:
I have a variable of type string. Is there any way to copy that to the
clipboard without having to place it in a cell first. The following code
does not work but should indicate what I am trying to do:
Dim s as String
s = "ABC"
s.Copy

Add a reference to your project to "Microsoft Forms 2.0 Object Library"
(if you haven't done this sort of thing before, see the Tools menu in
the VB editor).

Then you can work with the clipboard via a DataObject.

For example, to put the content of a string variable (txt) on the clipbard:

Dim myData as DataObject
Set myData = New DataObject
myData.SetText txt, 1
myData.PutInClipboard
 
THANKS! It works great.
I think if you use .Copy, you're going to get the line terminator no
matter what. You could strip the line terminator from the end of the string.

Another option is to get the text from A1 directly.

txt = Range("A1").Value

txt won't contain the line terminator. Then see below if you need to put
txt on the clipboard.


Add a reference to your project to "Microsoft Forms 2.0 Object Library"
(if you haven't done this sort of thing before, see the Tools menu in
the VB editor).

Then you can work with the clipboard via a DataObject.

For example, to put the content of a string variable (txt) on the clipbard:

Dim myData as DataObject
Set myData = New DataObject
myData.SetText txt, 1
myData.PutInClipboard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top