469,364 Members | 2,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

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 2401
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Hostile17 | last post: by
reply views Thread by Al Dente | last post: by
3 posts views Thread by SAL | last post: by
1 post views Thread by Larry Lard | last post: by
3 posts views Thread by Adam Honek | last post: by
5 posts views Thread by tony | last post: by
2 posts views Thread by Curious | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.