Solved

Clipboard.SetText

Posted on 2004-08-06
19
201 Views
Last Modified: 2008-02-01
HI,

I hope someone can help,
I work in a large call centre for which i have created an application to copy a reference number to the clipboard so it can be used in other applications.

On 99% of machines this works fine, but on some it doesn’t, but to make thing more confusing, on the machines it doesn’t work, it will work sometimes.

I have tried pasting the clipboard data into word, when i click Ctrl+V nothing is pasted, i can copy selected text from the word and then paste it back into other applications so i am pretty sure that the clipboard object is still functioning properly.

If anyone can help i would be most grateful.
Regards
Mark
0
Comment
Question by:markjcox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +3
19 Comments
 
LVL 14

Expert Comment

by:aelatik
ID: 11734486
Did you try it on a already started Word Application or did you start Word after you copied the text to the clipboard ?
0
 

Author Comment

by:markjcox
ID: 11734837
I opend a new word Application.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11735197
Odd thing:  Are you calling Clipboard.Clear first?

Bob
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:markjcox
ID: 11735339
Yes I am
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11735369
What are you copying to the clipboard--regular text, rich text?  In Word, is the Paste option enabled or disabled when you tried to paste using the Ctrl+V?

Bob
0
 

Author Comment

by:markjcox
ID: 11735412
I am copying regular Text (i am copying a string variable). In word the paste option is enabled, as is all the other application paste options.

Mark
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11735443
This really is starting to sound like "Ghosts in the Machine".  Is there a common trait on the 1% of machines that it doesn't work on (i.e. Developer machine, Windows XP without SP1, etc.)?

Bob
0
 

Author Comment

by:markjcox
ID: 11735494
I believe that most of the problem machines are NT, In the past applying SP6A has 'appeared' to fix the problem but on a few occasion it did'nt. I dont know if it make a difference but the agent logged in has a XP profile rather than an NT profile.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11735514
Does the problem depend on who is logged into that particular machine?

Bob
0
 

Author Comment

by:markjcox
ID: 11735566
No, I think the problem is machine related, although she is the only person using that machine for the past week (and it is only in the past week the problem has been passed on to me). I will not be able to reply to any post for a couple of hours as i am not going into a meeting, Thanks for your effort Bob.

Mark
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11735595
I would try to check to see if it is actually a machine-related or profile-related problem, by having someone log into the machine who works properly, and see if they have a problem on that machine.

Bob
0
 
LVL 2

Expert Comment

by:KJHDI12
ID: 11736402

Are she able to past the data into a simpler application, like notepad? If so there might be some characters in the string word reacts to. Also check that the keyboard setup (regional setup) of the computers is the same.
0
 
LVL 22

Accepted Solution

by:
DarkoLord earned 500 total points
ID: 11738700
Hmmmm.... try using APIs for accessing the clipboard... Here are two functions to do that:

Private Declare Function GetClipboardData Lib "User32" (ByVal wFormat As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function CloseClipboard Lib "User32" () As Long
Private Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "User32" () As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Private Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Const GHND = &H42
Private Const CF_TEXT = 1
Private Const MAXSIZE = 4096

Public 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 "Could not unlock memory location. Copy aborted."
GoTo OutOfHere2
End If

' Open the Clipboard to copy data to.
If OpenClipboard(0& = 0 Then
MsgBox "Could not open the Clipboard. Copy aborted."
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 "Could not close Clipboard."
End If

End Function

Private Sub Command0_Click()
ClipBoard_SetData ("To Clipboard")
End Sub

'-------------------------------------------------------------------------------------------------------------------------


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 "Cannot open Clipboard. Another app. may have it open"

      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 "Could not allocate memory"
      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 "Could not lock memory to copy string from."
   End If

OutOfHere:

   RetVal = CloseClipboard()
   ClipBoard_GetData = MyString

End Function


Darko
0
 
LVL 4

Expert Comment

by:cachedVB
ID: 11746355
I dont think it is a problem with vb6... it is like that for me on my computer: copy often doesnt work with Ctrl+C in most programs, so I need to do it like 5 times.
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 11746360
Well the functions in my post above use the clipboard APIs directly so it should work in all cases...

Darko
0
 
LVL 4

Expert Comment

by:cachedVB
ID: 12404498
I would recommend accepting DarkoLord's answer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question