Discussion:
Clipboard.GetText no longer working
(too old to reply)
Ed White
2007-03-16 14:57:08 UTC
Permalink
I have an application where I copy data onto the clipboard from Excel, and
then read the data from the clipboard in my application. This worked fine
until recently, where inexplicably, everytime I use the command to read the
clipboard, it comes up blank (Nothing or ""). For example,

Dim clipbdStr as String 'in declarations

'in both all of the following, clipbdStr returns Nothing
clipbdStr=My.Computer.Clipboard.GetText
clipbdStr=Clipboard.GetText

I am certain the clipboard has something in it before excuting the above, as
in Debug mode I can paste the contents onto Notepad before stepping through
the above commands, and I can as well see the area of Excel highlighted with
moving dashes around it.

I saw in the help menus something about permissions, although the
instructions to change them apply only to .Net 1 and I have .Net 2.

Why can't I read the clipboard anymore? I have the latest version of VB 2005
(SP.050727-7600). I can't tell what I changed since the last time I tried to
run this section of the code.
--
Ed
Ed White
2007-03-16 20:37:00 UTC
Permalink
I've isolated the problem. In the following code, I run the sub 'Run'
directly from sub Main, and the Clipboard.GetText works. When I run the
exact same sub, but on new thread, the Clipboard.GetText does not work. So
why won't Clipboard.GetText work when called from a new thread?????

Imports Microsoft.Office.Interop, System.Windows.Forms
Module Module1
Dim Exc As Microsoft.Office.Interop.Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim clipbdStr As String

Sub Main()
Run() 'Clipboard.GetText works

Dim th As New Threading.Thread(AddressOf Run)
th.Start() 'Clipboard.GetText does not work

End Sub 'Main

Sub Run()

Exc = New Excel.Application
Exc.Visible = True
wb = Exc.Workbooks.Add
ws = wb.Worksheets("Sheet1")
For r As Integer = 1 To 4
ws.Range("A" & r.ToString).Value = r
Next r
ws.Range("A1:A4").Copy()
clipbdStr = Clipboard.GetText

MsgBox("'" & clipbdStr & "'")
wb.Close(False)
Exc.Quit()

End Sub 'Run
End Module
--
Ed
Post by Ed White
I have an application where I copy data onto the clipboard from Excel, and
then read the data from the clipboard in my application. This worked fine
until recently, where inexplicably, everytime I use the command to read the
clipboard, it comes up blank (Nothing or ""). For example,
Dim clipbdStr as String 'in declarations
'in both all of the following, clipbdStr returns Nothing
clipbdStr=My.Computer.Clipboard.GetText
clipbdStr=Clipboard.GetText
I am certain the clipboard has something in it before excuting the above, as
in Debug mode I can paste the contents onto Notepad before stepping through
the above commands, and I can as well see the area of Excel highlighted with
moving dashes around it.
I saw in the help menus something about permissions, although the
instructions to change them apply only to .Net 1 and I have .Net 2.
Why can't I read the clipboard anymore? I have the latest version of VB 2005
(SP.050727-7600). I can't tell what I changed since the last time I tried to
run this section of the code.
--
Ed
Bryan Phillips
2007-03-17 05:19:52 UTC
Permalink
Why would you not read from the Excel spreadsheet directly? If you are
depending on the user to select cells, you can still use the Selection
object to get at them.

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Post by Ed White
I've isolated the problem. In the following code, I run the sub 'Run'
directly from sub Main, and the Clipboard.GetText works. When I run the
exact same sub, but on new thread, the Clipboard.GetText does not work. So
why won't Clipboard.GetText work when called from a new thread?????
Imports Microsoft.Office.Interop, System.Windows.Forms
Module Module1
Dim Exc As Microsoft.Office.Interop.Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim clipbdStr As String
Sub Main()
Run() 'Clipboard.GetText works
Dim th As New Threading.Thread(AddressOf Run)
th.Start() 'Clipboard.GetText does not work
End Sub 'Main
Sub Run()
Exc = New Excel.Application
Exc.Visible = True
wb = Exc.Workbooks.Add
ws = wb.Worksheets("Sheet1")
For r As Integer = 1 To 4
ws.Range("A" & r.ToString).Value = r
Next r
ws.Range("A1:A4").Copy()
clipbdStr = Clipboard.GetText
MsgBox("'" & clipbdStr & "'")
wb.Close(False)
Exc.Quit()
End Sub 'Run
End Module
--
Ed
Post by Ed White
I have an application where I copy data onto the clipboard from Excel, and
then read the data from the clipboard in my application. This worked fine
until recently, where inexplicably, everytime I use the command to read the
clipboard, it comes up blank (Nothing or ""). For example,
Dim clipbdStr as String 'in declarations
'in both all of the following, clipbdStr returns Nothing
clipbdStr=My.Computer.Clipboard.GetText
clipbdStr=Clipboard.GetText
I am certain the clipboard has something in it before excuting the above, as
in Debug mode I can paste the contents onto Notepad before stepping through
the above commands, and I can as well see the area of Excel highlighted with
moving dashes around it.
I saw in the help menus something about permissions, although the
instructions to change them apply only to .Net 1 and I have .Net 2.
Why can't I read the clipboard anymore? I have the latest version of VB 2005
(SP.050727-7600). I can't tell what I changed since the last time I tried to
run this section of the code.
--
Ed
Ed White
2007-03-18 21:25:03 UTC
Permalink
I believe I did some testing once and determined that copying from the
clipboard was faster than looping through each cell in Excel. Since I moved
my sub to a thread and the GetText from the clipboard doesn't work anymore, I
in fact changed the code to read directly from Excel.

Nonetheless, the point remains that there is no reason why Clipboard.GetText
should not work when called from a thread, and it took me a while to figure
out that this was the reason why my code was not working anymore. This is a
bug that should be fixed.
--
Ed
Post by Bryan Phillips
Why would you not read from the Excel spreadsheet directly? If you are
depending on the user to select cells, you can still use the Selection
object to get at them.
--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Post by Ed White
I've isolated the problem. In the following code, I run the sub 'Run'
directly from sub Main, and the Clipboard.GetText works. When I run the
exact same sub, but on new thread, the Clipboard.GetText does not work. So
why won't Clipboard.GetText work when called from a new thread?????
Imports Microsoft.Office.Interop, System.Windows.Forms
Module Module1
Dim Exc As Microsoft.Office.Interop.Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim clipbdStr As String
Sub Main()
Run() 'Clipboard.GetText works
Dim th As New Threading.Thread(AddressOf Run)
th.Start() 'Clipboard.GetText does not work
End Sub 'Main
Sub Run()
Exc = New Excel.Application
Exc.Visible = True
wb = Exc.Workbooks.Add
ws = wb.Worksheets("Sheet1")
For r As Integer = 1 To 4
ws.Range("A" & r.ToString).Value = r
Next r
ws.Range("A1:A4").Copy()
clipbdStr = Clipboard.GetText
MsgBox("'" & clipbdStr & "'")
wb.Close(False)
Exc.Quit()
End Sub 'Run
End Module
--
Ed
Post by Ed White
I have an application where I copy data onto the clipboard from Excel, and
then read the data from the clipboard in my application. This worked fine
until recently, where inexplicably, everytime I use the command to read the
clipboard, it comes up blank (Nothing or ""). For example,
Dim clipbdStr as String 'in declarations
'in both all of the following, clipbdStr returns Nothing
clipbdStr=My.Computer.Clipboard.GetText
clipbdStr=Clipboard.GetText
I am certain the clipboard has something in it before excuting the above, as
in Debug mode I can paste the contents onto Notepad before stepping through
the above commands, and I can as well see the area of Excel highlighted with
moving dashes around it.
I saw in the help menus something about permissions, although the
instructions to change them apply only to .Net 1 and I have .Net 2.
Why can't I read the clipboard anymore? I have the latest version of VB 2005
(SP.050727-7600). I can't tell what I changed since the last time I tried to
run this section of the code.
--
Ed
WenYuan Wang
2007-03-19 13:15:24 UTC
Permalink
Hi Ed,
I'm sorry to hear you have such inconvenience experience with Visual Studio.

As far as I know, the Clipboard class can only be used in threads set to
single thread apartment (STA) mode.
http://msdn2.microsoft.com/en-us/library/kz40084e(vs.80).aspx
[Clipboard.GetText Method ()]

I surprised you hadn't received the 'System.Threading.ThreadStateException'
Exception when calling Clipboard.GetText() method in the new thread. (Did
you catch this exception?)

However, I'm afraid this is not a product issue. I suggest you may specify
the apartment state of the thread as single-threaded as following.

Dim th As New Threading.Thread(AddressOf Run2)
th.SetApartmentState(Threading.ApartmentState.STA)
th.Start()

Please feel free to update here if there is anything I can help with. I'm
glad to assist you.

Hope this helps
Sincerely,
Wen Yuan
Ed White
2007-03-19 16:38:03 UTC
Permalink
I modified the code to add the STA, and it works. Thanks much--that's what I
was looking for.

No, I do not get a System.Threading.ThreadStateException when I ran the code
I listed above. Instead, when the threaded version runs, the MessageBox
window simply shows two apostrophes, i.e. '', instead of
'
1
2
3
4
'
when Run() is run directly instead of through the thread. Had the exception
been thrown, perhaps I could have figured out what to do from the exception
message. So, while you're correct in stating that, as documented,
Clipboard.GetText method should only be run in STA mode, for some reason an
exception apparently should be thrown and it's not.

Thanks again for your help.
--
Ed
Post by WenYuan Wang
Hi Ed,
I'm sorry to hear you have such inconvenience experience with Visual Studio.
As far as I know, the Clipboard class can only be used in threads set to
single thread apartment (STA) mode.
http://msdn2.microsoft.com/en-us/library/kz40084e(vs.80).aspx
[Clipboard.GetText Method ()]
I surprised you hadn't received the 'System.Threading.ThreadStateException'
Exception when calling Clipboard.GetText() method in the new thread. (Did
you catch this exception?)
However, I'm afraid this is not a product issue. I suggest you may specify
the apartment state of the thread as single-threaded as following.
Dim th As New Threading.Thread(AddressOf Run2)
th.SetApartmentState(Threading.ApartmentState.STA)
th.Start()
Please feel free to update here if there is anything I can help with. I'm
glad to assist you.
Hope this helps
Sincerely,
Wen Yuan
WenYuan Wang
2007-03-20 04:21:51 UTC
Permalink
You are welcome, Ed.
Sorry, I'm not sure why run method doesn't thrown the exception for you. I
think this is dependent on your code. However, I'm very glad to hear your
issue has been resolved.:) Please don't hesitate to let me know, if you
meet any further issue. I'm glad to assist you.

Have a great day.
Sincerely,
Wen Yuan

Loading...