Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable value copied to clipboard 1

Status
Not open for further replies.

scottian

Programmer
Jul 3, 2003
955
GB
is there a way to set the value of a variable to the contents of the clipboard?

"My God! It's full of stars...
 
I'm not sure, when it comes to pasting a value from the clipboard directly to a variable. But, if you could "go thru" using a text control, then the accmdpaste arguement of the runcommand method of the docmd object might do, perhaps something like this:

[tt]me!txtPaste.SetFocus
docmd.runcommand accmdpaste
MyPublic = me!txtPaste.Value[/tt]

- I think the control needs to be visible, but you could make it very small, place another object on top of it...

Roy-Vidar
 
bill power did something about copying to the clipboard and I think I copied it? I will do a search later but it might be on his site

Hope this helps
Hymn
 
sorry my fault,
what i meant to ask was is there a way to set the clipboard contents to the value of a variable

"My God! It's full of stars...
 
Text controls? Just reverse the previous, place the variable contents in a text control, and use the accmdcopy arguement (test it by hitting ctrl+v in for instance Notepad).

Roy-Vidar
 
Code:
Const CF_TEXT = 1
Const GMEM_MOVEABLE = &H2
Const GMEM_ZEROINIT = &H40
Const GHND = &H42

Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal 
dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function OpenClipboard Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "USER32" () As Long
Private Declare Function EmptyClipboard Lib "USER32" () As Long
Private Declare Function SetClipboardData Lib "USER32" (ByVal wFormat As Long, ByVal 
hMem As Long) As Long
Private Declare Function GetClipboardData Lib "USER32" (ByVal wFormat As Long) As Long
Private Declare Sub CopyMemoryLS Lib "Kernel32.dll" Alias "RtlMoveMemory" (ByVal dest 
As Any, ByVal src As Any, ByVal n As Long)

'********** END WINDOWS CLIPBOARD DECLARATIONS **********


Function ClipBoard_SetText(strCopyString As String) As Boolean

Dim hGlobalMemory As Long, lpGlobalMemory As Long, hClipMemory As Long

   ' Allocate moveable global memory
   hGlobalMemory = GlobalAlloc(GHND, Len(strCopyString) + 1)
   ' Lock block to get far pointer to this memory
   lpGlobalMemory = GlobalLock(hGlobalMemory)
   ' Copy the string to global memory
   CopyMemoryLS lpGlobalMemory, strCopyString, Len(strCopyString)
   ' Unlock memory then copy to clipboard
   If GlobalUnlock(hGlobalMemory) = 0 Then
      If OpenClipboard(0&) <> 0 Then
      'If OpenClipboard(Screen.ActiveForm.Hwnd) <> 0 Then   'can't use when debugging
         Call EmptyClipboard
         hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
         ClipBoard_SetText = CBool(CloseClipboard)
      End If
   End If
End Function

Function ClipBoard_GetText() As String                                 

   Dim hClipMemory As Long, lpClipMemory As Long, strCBText As String
   Dim RetVal As Long, lngSize As Long
   
   If OpenClipboard(0&) <> 0 Then
   'If OpenClipboard(Screen.ActiveForm.Hwnd) <> 0 Then      'can't use when debugging
   'Get handle to global memory block that is referencing the text
      hClipMemory = GetClipboardData(CF_TEXT)
      If hClipMemory <> 0 Then
         'Lock Clipboard memory so we can reference the actual data string
         lpClipMemory = GlobalLock(hClipMemory)
         If lpClipMemory <> 0 Then
            lngSize = GlobalSize(lpClipMemory)              'size of string in 
clipboard
            strCBText = Space$(lngSize)                     'make VBA string to hold 
clipboard data
            CopyMemoryLS strCBText, lpClipMemory, lngSize   'copy from clipboard to 
our string
            RetVal = GlobalUnlock(hClipMemory)              'unlock the memory
            'Remove the null terminating character
            strCBText = Left(strCBText, InStr(1, strCBText, Chr$(0), 0) - 1)
         Else
            MsgBox "Could not lock memory to copy string from."
         End If
      End If
      Call CloseClipboard     'close the clipboard
   End If
   ClipBoard_GetText = strCBText
End Function

Use ClipBoard_SetText to pass data to the clipboard and ClipBoard_GetText to retrieve it.

Craig
 
Craig0201 has posted code that is similar to Bill Powers so I will leave it at that

Hope this helps
Hymn
 
Craig,
sorry to be a pain, but does the above code go into a module or a form? and how do i use the "ClipBoard_SetText" do i call it from a macro or event?

"My God! It's full of stars...
 
scottian,

Paste the above code in a module.

You then call Clipboard_SetText and Clipboard_GetText as a function in your code like any other function.

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top