Can't get around STAThreadAttribute

G

Guest

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?
 
D

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.
 
G

Guest

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.
 
S

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?
 
B

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)
 
G

Guest

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.
 
D

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
 
G

Guest

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
 
D

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
 

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