473,395 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can't get around STAThreadAttribute

SAL
When the following code is excuted from my .apsx page:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim test As New MyTestClass

'Create an Instance of the ClipBoard Object
Dim sHyperLink As String

Dim data_object As New System.Windows.Forms.DataObject
Dim DataFormats As System.Windows.Forms.DataFormats
Dim objClipboard As System.Windows.Forms.Clipboard

sHyperLink = test.CreateHyperlink(CInt(TextBox1.Text), TextBox2.Text)

'Write HyperLink to Clip Board
data_object.SetData(DataFormats.Html, True, sHyperLink)
objClipboard.SetDataObject(data_object, True)
End Sub

I get this error:

The current thread must set to Single Thread Apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.

Since I'm trying to place items on the Clients Clipboard, is there a way
around the System.Windows.Forms Namespace and still be able to use these
classes I need? If not, how do I get around the STAThreadAttribute error?
Feb 10 '06 #1
10 2466
Why don't you use javascript?

http://dotnet.org.za/eduard/archive/...7/29/3064.aspx

HTH,
Darren Kopp
http://blog.secudocs.com/

Feb 10 '06 #2
I guess that doesn't set the data object though. However, I don't
think that will work for people connecting over a website.

Feb 10 '06 #3
SAL
I have this JavaScript function and it works, but it doesn't set the
formatting that I am looking for:

/// Copy Source to Clipboard
function copytoClipBoard(rSource)
{
if(rSource!= "")
window.clipboardData.setData("Text",rSource);
}

When I copy the hyperlink to the Clipboard I want it to retain it's HTML
formatting. Basically, I'm trying to capture something like this

http://www.somewhere.com as MyLink

where I want only MyLink to show up (in blue with a underline and the
codebehind is the url as the hyperlink). When I do a Ctrl+P in a Microsoft
Word Document it should just paste MyLink with a blue underline ONLY.

System.Windows.Forms.DataObject should allow me to do that, but I don't know
how to get it to work with .aspx, since it appears to only work with Windows
Forms.

"Darren Kopp" wrote:
Why don't you use javascript?

http://dotnet.org.za/eduard/archive/...7/29/3064.aspx

HTH,
Darren Kopp
http://blog.secudocs.com/

Feb 10 '06 #4
You have to use AspCompat="true" in your @ Page directive to get an
STA thread for your page.

But ....

You know you'll be manipulating the clipboard of the server, right?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 10 Feb 2006 09:19:30 -0800, SAL
<SA*@discussions.microsoft.com> wrote:
When the following code is excuted from my .apsx page:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim test As New MyTestClass

'Create an Instance of the ClipBoard Object
Dim sHyperLink As String

Dim data_object As New System.Windows.Forms.DataObject
Dim DataFormats As System.Windows.Forms.DataFormats
Dim objClipboard As System.Windows.Forms.Clipboard

sHyperLink = test.CreateHyperlink(CInt(TextBox1.Text), TextBox2.Text)

'Write HyperLink to Clip Board
data_object.SetData(DataFormats.Html, True, sHyperLink)
objClipboard.SetDataObject(data_object, True)
End Sub

I get this error:

The current thread must set to Single Thread Apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.

Since I'm trying to place items on the Clients Clipboard, is there a way
around the System.Windows.Forms Namespace and still be able to use these
classes I need? If not, how do I get around the STAThreadAttribute error?


Feb 10 '06 #5
this still will not work because the clipboard is part of the desktop. there
is no desktop for services like asp.net. not sure how putting data in the
servers clipboard would do any good anyway.

-- bruce (sqlwork.com)

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:sd********************************@4ax.com...
You have to use AspCompat="true" in your @ Page directive to get an
STA thread for your page.

But ....

You know you'll be manipulating the clipboard of the server, right?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 10 Feb 2006 09:19:30 -0800, SAL
<SA*@discussions.microsoft.com> wrote:
When the following code is excuted from my .apsx page:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim test As New MyTestClass

'Create an Instance of the ClipBoard Object
Dim sHyperLink As String

Dim data_object As New System.Windows.Forms.DataObject
Dim DataFormats As System.Windows.Forms.DataFormats
Dim objClipboard As System.Windows.Forms.Clipboard

sHyperLink = test.CreateHyperlink(CInt(TextBox1.Text),
TextBox2.Text)

'Write HyperLink to Clip Board
data_object.SetData(DataFormats.Html, True, sHyperLink)
objClipboard.SetDataObject(data_object, True)
End Sub

I get this error:

The current thread must set to Single Thread Apartment (STA) mode before
OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.

Since I'm trying to place items on the Clients Clipboard, is there a way
around the System.Windows.Forms Namespace and still be able to use these
classes I need? If not, how do I get around the STAThreadAttribute error?

Feb 11 '06 #6
SAL
Currently, my one line of JavaScript code does copy to the Clients Desktop
Clipboard, but it does not retain the formatting that that I looking for.

The formatting that I’m looking for is to capture the Embedded Hyperlink
(i.e. TestHyperLink in Blue with underline) and not just the URL, that I can
do a Ctrl+P into a Word Doc or Email.

Do you have any suggestion on how to do that?

If you go to a webpage that has an Embedded hyperlink you can high light
Embedded Hyperlink and do a Ctrl+C and then a Ctrl+P into a Work Doc, and
that works fine, but how do you do it programmatically is my question. My
Embedded Hyperlinks are being built from records in a DB table.

"Bruce Barker" wrote:
this still will not work because the clipboard is part of the desktop. there
is no desktop for services like asp.net. not sure how putting data in the
servers clipboard would do any good anyway.

-- bruce (sqlwork.com)

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:sd********************************@4ax.com...
You have to use AspCompat="true" in your @ Page directive to get an
STA thread for your page.

But ....

You know you'll be manipulating the clipboard of the server, right?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 10 Feb 2006 09:19:30 -0800, SAL
<SA*@discussions.microsoft.com> wrote:
When the following code is excuted from my .apsx page:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim test As New MyTestClass

'Create an Instance of the ClipBoard Object
Dim sHyperLink As String

Dim data_object As New System.Windows.Forms.DataObject
Dim DataFormats As System.Windows.Forms.DataFormats
Dim objClipboard As System.Windows.Forms.Clipboard

sHyperLink = test.CreateHyperlink(CInt(TextBox1.Text),
TextBox2.Text)

'Write HyperLink to Clip Board
data_object.SetData(DataFormats.Html, True, sHyperLink)
objClipboard.SetDataObject(data_object, True)
End Sub

I get this error:

The current thread must set to Single Thread Apartment (STA) mode before
OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it.

Since I'm trying to place items on the Clients Clipboard, is there a way
around the System.Windows.Forms Namespace and still be able to use these
classes I need? If not, how do I get around the STAThreadAttribute error?


Feb 13 '06 #7
What are you copying with the javascript? I am not sure exactly how
Word or the Email interpret what is a link, but they usually auto
format.

I would think you would need to copy all the <a
href="link.htm">title</a>, rather than just the href, or just the title
portions of the link.

Maybe you can post the javascript function.

-Darren Kopp
http://blog.secudocs.com

Feb 15 '06 #8
SAL
The javaScript function is nothing fancy. Here's what I have:

function copytoClipBoard(rSource)
{
if(rSource!= "")
window.clipboardData.setData("Text",rSource);
}

I just capture and pass the following string to my javaScript funtion
HyperLink = "<a href="www.someurl.com>SomeCaption</a>"

Which works fine when you do a response.write to a ASP.net page, but not
Word or Email. With Word or Email when you do a Ctrl+P you just get the URL.

However, if you go to some web page like MSDN, and "HIGHLIGHT" a hyperlink
and do a Ctrl+C then do a Ctrl+P, Word does Hyperlinks something like this:
{HYPERLINK "http://msdn.microsoft.com/default.aspx"}, which is the "Field
Codes" that you DO NOT see unless you turn it on. Instead, what you see is
the caption "MSDN Home" when you do a Ctrl+P. If you try to do this
programmatically there is no place for you put the Caption you want, so
instead you would get the whole URL in Blue and Underlined as I do when I use
Anchor Tags.

Sal

"Darren Kopp" wrote:
What are you copying with the javascript? I am not sure exactly how
Word or the Email interpret what is a link, but they usually auto
format.

I would think you would need to copy all the <a
href="link.htm">title</a>, rather than just the href, or just the title
portions of the link.

Maybe you can post the javascript function.

-Darren Kopp
http://blog.secudocs.com

Feb 15 '06 #9
alt-f9 allows you to toggle the field codes, but I wasn't able to
determine how to change the display text. Even with that, copying and
pasting a code such as {HYPERLINK ...} just pasted as straight text, so
I don't know how you can have it formatted how you want. Most I would
guess is a possible way to format what you copied using the word
activex object, but I don't know if it's possible or if it is possible
what you would need to do.

Regards,
Darren Kopp

Feb 15 '06 #10
I don't know if this will get you further than it got me, but you might
check out http://sbarnhill.mvps.org/WordFAQs/HyperlinkProbs.htm.

-Darren Kopp

Feb 15 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Hostile17 | last post by:
I've arrived at a compromise whereby I use valid HTML 4.01, *except* that I want to use the nonstandard "bordercolor" attribute for tables. My code validates just fine except for this...
0
by: Al Dente | last post by:
Round round get around I get around Yeah Get around round round I get around I get around Get around round round I get around From town to town Get around round round I get around I'm a real...
3
by: SAL | last post by:
I have the following VB.net Class/Function that I call from an ASP.net page: Public Class MyTestClass Public Function embedHyperlink(ByVal fileID As Integer, ByVal fileName As String) As String...
1
by: Larry Lard | last post by:
Just now I translated Jon Skeet's WaitHandle.WaitAny / .WaitAll (<http://yoda.arachsys.com/csharp/threads/waithandles.shtml>) from C# into VB.NET, and after the first go I ran into trouble: ...
3
by: Adam Honek | last post by:
How would I enable STAThreadAttribute in my Main function. I assume the error means Sub Main(). Apparently this may fix my openfiledialog error so it's worth a go. Thanks, Adam
5
by: tony | last post by:
I'm using PHP 5 on Win-98 command line (ie no web server involved) I'm processing a large csv file and when I loop through it I can process around 275 records per second. However at around...
1
by: fiaolle | last post by:
Hi I Can't get the MSDN's example for Listbox to work.The example is below. I get an error in Listbox's sub ListBox1_SelectedValueChanged at row textBox1.Text = ListBox1.SelectedValue and it...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
2
by: Curious | last post by:
I got the error when I execute the following line of code: Clipboard.SetDataObject(mGrid.Clip); The error is: "Current thread must be set to single thread apartment (STA) mode before OLE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.