PDA

View Full Version : [SOLVED] Ctrl-C selection in memory - how work with it by VBA Excel code?



Juraj
07-27-2017, 05:02 PM
I have Form1 with TextBox1. I made TextBox1.Text selection - manually with mouse and Ctrl-C or by code

TextBox1.SelStart = 0
TextBox1.SelLength = TextBox1.TextLength
TextBox1.Copy

It means that value TextBox1.Text is copied in memory in some buffer.

My questions:

1. How can I copy this value from memory to some string type variable S? Note: Forgot the S = TextBox1.Text solution, my problem is to read the value from memory.

2. How can I clean this selected value from memory by some code? It means Ctrl-V will return nothing (zero string).

Logit
07-27-2017, 08:29 PM
.
To clear the Clip Board, most websites for VBA recommend using : Application.CutCopyMode=False placed as the last line in your macro code.

Or you can utilize this function placed into a Routine Module:



Option Explicit
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long

Public Function ClearClipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Function

Sub ccc()
Call ClearClipboard
End Sub


At the end of your macro code, to clear the clipboard, 'call' the above function with : Call ccc
although it is not necessary to use the word Call - you can simply type ccc

See the attached workbook for a demonstration.

To copy/paste something from the textbox to another location in your workbook you can use a macro code something like this :



Option Explicit


Private Sub CommandButton1_Click()

Sheets("Sheet1").Range("A1").Value = Me.TextBox1.Text
Label1.Caption = Me.TextBox1.Text
Label2.Visible = True
Sheets("Sheet2").Range("A1:A10").Value = Me.TextBox1.Text
Label3.Visible = True

ccc
End Sub




Run the UserForm in the workbook and see.

p45cal
07-28-2017, 03:19 AM
How can I copy this value from memory to some string type variable S? Note: Forgot the S = TextBox1.Text solution, my problem is to read the value from memory.
See http://wellsr.com/vba/2015/tutorials/vba-copy-to-clipboard-paste-clear/ about 1/3rd down, section headed VBA Paste from Clipboard

Juraj
07-28-2017, 03:58 AM
Thank you for answers both of you. That's it what I needed :-)

Logit
07-28-2017, 08:20 AM
You are welcome.