Home All Groups Group Topic Archive Search About

Can't get around STAThreadAttribute

Author
10 Feb 2006 5:19 PM
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?

Author
10 Feb 2006 5:31 PM
Darren Kopp
Are all your drivers up to date? click for free checkup

Author
10 Feb 2006 6:01 PM
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.

Show quoteHide quote
"Darren Kopp" wrote:

> Why don't you use javascript?
>
> http://dotnet.org.za/eduard/archive/2004/07/29/3064.aspx
>
> HTH,
> Darren Kopp
> http://blog.secudocs.com/
>
>
Author
10 Feb 2006 5:36 PM
Darren Kopp
I guess that doesn't set the data object though.  However, I don't
think that will work for people connecting over a website.
Author
10 Feb 2006 10:22 PM
Scott Allen
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
<S**@discussions.microsoft.com> wrote:

Show quoteHide quote
>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?
Author
11 Feb 2006 12:50 AM
Bruce Barker
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)

Show quoteHide quote
"Scott Allen" <scott@nospam.odetocode.com> wrote in message
news:sd4qu1p753r81rofcltee3u2ds7f1clcpi@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
> <S**@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?
>
Author
13 Feb 2006 2:28 PM
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.

Show quoteHide quote
"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" <scott@nospam.odetocode.com> wrote in message
> news:sd4qu1p753r81rofcltee3u2ds7f1clcpi@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
> > <S**@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?
> >
>
>
>
Author
15 Feb 2006 3:16 AM
Darren Kopp
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
Author
15 Feb 2006 4:31 PM
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

Show quoteHide quote
"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
>
>
Author
15 Feb 2006 8:07 PM
Darren Kopp
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
Author
15 Feb 2006 8:08 PM
Darren Kopp
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

Bookmark and Share