Solved

Clipboard.SetText

Posted on 2004-08-06
19
179 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
  • 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now