Can't Paste

D

dsc

I have a user form with a text box and a list box. I found some code on the
net and put it into my macro like this:

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

If ListBox1.CanPaste = True Then
TextBox1 = ListBox1.Value
Else
TextBox1.Text = "Can't Paste"
End If
End Sub

Works fine on all my machines at home, and on all the machines at work
except one. That one always goes to "Else" TextBox1.Text = "Can't Paste"

Does anyone have any idea why this would be happening?

Thanks for any assistance.
 
J

Jay Freedman

I have a user form with a text box and a list box. I found some code on the
net and put it into my macro like this:

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

If ListBox1.CanPaste = True Then
TextBox1 = ListBox1.Value
Else
TextBox1.Text = "Can't Paste"
End If
End Sub

Works fine on all my machines at home, and on all the machines at work
except one. That one always goes to "Else" TextBox1.Text = "Can't Paste"

Does anyone have any idea why this would be happening?

Thanks for any assistance.

I think you don't understand the bit of code you found on the net.
This stuff makes no sense at all...

First, the .CanPaste method, according to the VBA help, "Specifies
whether the Clipboard contains data that the object supports." (In
this case, "the object" refers to ListBox1.) Since you aren't doing
anything with the clipboard, this is completely useless to you.

Second, I'm amazed you can get this code to run at all. When I try it,
I get a runtime error pointing to "ListBox1.CanPaste" saying the
object doesn't support that method. Sure enough, the help says the
..CanPaste method applies to combo boxes and text boxes -- since you
can't paste into a list box, "can paste" makes no sense there.

I suspect what you want is something like this:

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If ListBox1.Value <> "" Then
TextBox1 = ListBox1.Value
Else
TextBox1.Text = "No value"
End If
End Sub

In fact, a better scheme is to include this line in the
UserForm_Initialize procedure, after you add the items to the listbox:

ListBox1.ListIndex = 0

That will put the highlight immediately on the first item in the list,
and the user won't be able to double-click anywhere in the listbox
except on one of the existing items. Then you don't need the
If...Else...End If, just the TextBox1 = ListBox1.Value.
 
D

dsc

I think you don't understand the bit of code you found on the net.
This stuff makes no sense at all...

It don't surprise me none, as Cowboy said in Full Metal Jacket. I often try
to get stuff I don't understand to work, because I'm (a) busy, and (b)
iggernunt.
Second, I'm amazed you can get this code to run at all.

It does run, on both Word 2000 and Word XP. After reading your note, I tried
to run the macro with nothing in the clipboard, and I see that you are
correct. And that was the problem: purely by chance, every computer I
checked it on had text in the clipboard, except one.

I'll implement your solution when I get to work Monday. Thanks a million.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top