none
Copy URL into Clipboard

    Question

  • Hi

    I want to copy an URL into Clipboard let's say http://msdn.com and if i paste it to MS-Word it should be in URL format (blue highlighted with underlined), how to get this done? I tried

    Clipboard.SetData(DataFormats.Html, "<a href ='http://msdn.com'> MS </a>");

    and many more ... please let me know how to get this done?
    Thursday, April 16, 2009 4:22 PM

Answers

  • This is a pretty complicated question.  The format of the text, where it's pasted, depends on where you're pasting it to.  For example, when you copy something from MS-Word, and you paste it into the text editor for this site, it works just fine, but if you try to paste it into a blog using blogger, it won't render correctly.  Different locations will pull different data from the clipboard.  In other words, the Clipboard can hold several different versions of the same piece of text.  Try it:

    Write some code in visual studio, then copy and paste that code to the editor here.  You'll get something like this:

    static

    void Main()

    {

     

    Application.EnableVisualStyles();

     

    Application.SetCompatibleTextRenderingDefault(false);

     

    Application.Run(new Form1());

    }

     

    Next, instead of pasting it from Visual Studio, paste it into Word first, and change the font size:

    static void Main()

    {

        Application.EnableVisualStyles();

        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new Form1());

    }

    Looks better, doesn't it?  This is because when you copy from Visual Studio, it sets formats differently than Word.  Word, frankly, sets it better for this situation. 

    Now, with both of them, you can paste the information into word, and you only get the text. 

    Okay, now that you've had that explanation, you can get all the formats currently copied to the clipboard, by doing something like this:

    IDataObject data = Clipboard.GetDataObject();

    string[] formats = data.GetFormats();

    foreach (string format in formats)

        Console.WriteLine(format);

    Now, this will print the formats available.  Notice that some of them are different than others.  You can fetch the information for each one by calling: Clipboard.GetData(string format) and passing in one of the format strings defined above.  Beware, however, some will throw irrevocable errors in your application and stop you whether or not you have a try/catch. 

    Now, when you copy this data, certain sites will request certain things.  This site, for instance, requests the HTML Format, thus giving the nicely formatted text from Word.  The HTML Format from word is much better than Visual Studio.

    Some applications will format hyperlinks for you, however.  Simply paste the text, and try adding a space after it.  Word should do this for you.

    Hopefully this gives you some idea of how to move forward.  Obviously, from what you can tell, it's not a straightforward answer, because each situation is going to be different. 

    Good luck.


    David Morton - http://blog.davemorton.net/
    Thursday, April 16, 2009 4:34 PM

All replies

  • This is a pretty complicated question.  The format of the text, where it's pasted, depends on where you're pasting it to.  For example, when you copy something from MS-Word, and you paste it into the text editor for this site, it works just fine, but if you try to paste it into a blog using blogger, it won't render correctly.  Different locations will pull different data from the clipboard.  In other words, the Clipboard can hold several different versions of the same piece of text.  Try it:

    Write some code in visual studio, then copy and paste that code to the editor here.  You'll get something like this:

    static

    void Main()

    {

     

    Application.EnableVisualStyles();

     

    Application.SetCompatibleTextRenderingDefault(false);

     

    Application.Run(new Form1());

    }

     

    Next, instead of pasting it from Visual Studio, paste it into Word first, and change the font size:

    static void Main()

    {

        Application.EnableVisualStyles();

        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new Form1());

    }

    Looks better, doesn't it?  This is because when you copy from Visual Studio, it sets formats differently than Word.  Word, frankly, sets it better for this situation. 

    Now, with both of them, you can paste the information into word, and you only get the text. 

    Okay, now that you've had that explanation, you can get all the formats currently copied to the clipboard, by doing something like this:

    IDataObject data = Clipboard.GetDataObject();

    string[] formats = data.GetFormats();

    foreach (string format in formats)

        Console.WriteLine(format);

    Now, this will print the formats available.  Notice that some of them are different than others.  You can fetch the information for each one by calling: Clipboard.GetData(string format) and passing in one of the format strings defined above.  Beware, however, some will throw irrevocable errors in your application and stop you whether or not you have a try/catch. 

    Now, when you copy this data, certain sites will request certain things.  This site, for instance, requests the HTML Format, thus giving the nicely formatted text from Word.  The HTML Format from word is much better than Visual Studio.

    Some applications will format hyperlinks for you, however.  Simply paste the text, and try adding a space after it.  Word should do this for you.

    Hopefully this gives you some idea of how to move forward.  Obviously, from what you can tell, it's not a straightforward answer, because each situation is going to be different. 

    Good luck.


    David Morton - http://blog.davemorton.net/
    Thursday, April 16, 2009 4:34 PM
  • Try adding a space or new line in the word document after you paste the URL. Word will format the hyperlink once it confirms you have finished editing the text.
    Hope It Works, Guna
    Thursday, April 16, 2009 4:41 PM
  • Hi,
    Try DataFormats.UnicodeText its working!
    Adil Mughal - MCP http://adilamughal.blogspot.com
    Thursday, April 16, 2009 4:50 PM