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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy to Clipboard 4

Status
Not open for further replies.

baronvont

Technical User
May 15, 2001
77
AU
Am I missing something. I have trawled thru the posts and came up with this...

Private Sub mnuCopy_Click()
Clipboard.Clear
Clipboard.SetText Text1.SelText
End Sub

I modified it to :

Private Sub Mysub()

Dim Mytext as string
Mytext = "sometext"

Clipboard.Clear
Clipboard.SetText Mytext

End Sub


I added it to my code, it runs thru it fine, but does not copy anything to the clipboard

Any help please!!!

Thanks
Georg
 
Dim Mytext as string
Mytext = "sometext"

Clipboard.Clear
Clipboard.SetText Mytext

End Sub

----------------------------

You may want to check your variable is actually holding your intended text.

Try implementing a msgbox after where you set your variable ,'Mytext', to equal something. Then get the msgbox to display the text.

If it displays the text then you know it is the 'Clipboard' part of the code, else the error could be where you set your variable to equal something.

I know this doesnt help as such, but it will help you locate the actual area where the bug is.

- F8i
 
Checked all that and the variable definately has the text in it... just not adding it to clipboard
 
Hi, try this Module.


Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
ByVal dwBytes As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
As Long
Declare Function EmptyClipboard Lib "User32" () As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
ByVal lpString2 As Any) As Long
Declare Function SetClipboardData Lib "User32" (ByVal wFormat _
As Long, ByVal hMem As Long) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096



Function ClipBoard_SetData(MyString As String)
Dim hGlobalMemory As Long, lpGlobalMemory As Long
Dim hClipMemory As Long, X As Long

' Allocate moveable global memory.
'-------------------------------------------
hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)

' Lock the block to get a far pointer
' to this memory.
lpGlobalMemory = GlobalLock(hGlobalMemory)

' Copy the string to this global memory.
lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)

' Unlock the memory.
If GlobalUnlock(hGlobalMemory) <> 0 Then
MsgBox &quot;Could not unlock memory location. Copy aborted.&quot;
GoTo OutOfHere2
End If

' Open the Clipboard to copy data to.
If OpenClipboard(0&) = 0 Then
MsgBox &quot;Could not open the Clipboard. Copy aborted.&quot;
Exit Function
End If

' Clear the Clipboard.
X = EmptyClipboard()

' Copy the data to the Clipboard.
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)

OutOfHere2:

If CloseClipboard() = 0 Then
MsgBox &quot;Could not close Clipboard.&quot;
End If

End Function

Best Regards

---
JoaoTL
NOSPAM_mail@jtl.co.pt
 
Many thanks, I stumbled across this yesterday and it works fine

Cheers
Georg
 
JoaoTL:

Your function to write a string to the clipboard is
great! I have given you a star. Do you also have
a function to read a string from the clipboard?

vbMax
 
Hi, retriving data from the clipboard

Declare Function OpenClipboard Lib &quot;User32&quot; (ByVal hwnd As Long) _
As Long
Declare Function CloseClipboard Lib &quot;User32&quot; () As Long
Declare Function GetClipboardData Lib &quot;User32&quot; (ByVal wFormat As _
Long) As Long
Declare Function GlobalAlloc Lib &quot;kernel32&quot; (ByVal wFlags&, ByVal _
dwBytes As Long) As Long
Declare Function GlobalLock Lib &quot;kernel32&quot; (ByVal hMem As Long) _
As Long
Declare Function GlobalUnlock Lib &quot;kernel32&quot; (ByVal hMem As Long) _
As Long
Declare Function GlobalSize Lib &quot;kernel32&quot; (ByVal hMem As Long) _
As Long
Declare Function lstrcpy Lib &quot;kernel32&quot; (ByVal lpString1 As Any, _
ByVal lpString2 As Any) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096


Function ClipBoard_GetData()
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim MyString As String
Dim RetVal As Long

If OpenClipboard(0&) = 0 Then
MsgBox &quot;Cannot open Clipboard. Another app. may have it open&quot;
Exit Function
End If

' Obtain the handle to the global memory
' block that is referencing the text.
hClipMemory = GetClipboardData(CF_TEXT)
If IsNull(hClipMemory) Then
MsgBox &quot;Could not allocate memory&quot;
GoTo OutOfHere
End If

' Lock Clipboard memory so we can reference
' the actual data string.
lpClipMemory = GlobalLock(hClipMemory)

If Not IsNull(lpClipMemory) Then
MyString = Space$(MAXSIZE)
RetVal = lstrcpy(MyString, lpClipMemory)
RetVal = GlobalUnlock(hClipMemory)

' Peel off the null terminating character.
MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
Else
MsgBox &quot;Could not lock memory to copy string from.&quot;
End If

OutOfHere:

RetVal = CloseClipboard()
ClipBoard_GetData = MyString

End Function



Best Regards

---
JoaoTL
NOSPAM_mail@jtl.co.pt
 
I did this little application to display IP addresses in a small form for users to easy get their ip address...

I used:

Private Sub mnuCopy_Click()
Dim IP As String
IP = Winsock1.LocalIP
Clipboard.Clear
Clipboard.SetText IP
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top