简体   繁体   中英

Clipboard.SetText(HTML) no longer work

I had a code which was adding certain generated string between HTML Tags and setting up the clipboard with HTML data. If I am not insane yet I am certain this code worked for me before, no changes have been made to it, but it doesn't work anymore!

I found the documentation of Clipboard HTML and took the example, made the test below and it does't work for me either.

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    Dim meh As String = _
            "Version:0.9" & vbCrLf & _
            "StartHTML:71" & vbCrLf & _
            "EndHTML:160" & vbCrLf & _
            "StartFragment:130" & vbCrLf & _
            "EndFragment:150" & vbCrLf & _
            "StartSelection:130" & vbCrLf & _
            "EndSelection:150" & vbCrLf & _
            "<!DOCTYPE html>" & vbCrLf & _
            "<HTML> " & vbCrLf & _
            "<BODY> " & vbCrLf & _
            "<!--StartFragment-->" & vbCrLf & _
            "<B>bold.</B> <I><B>This is bold italic.</B></I> <I>This</I> " & vbCrLf & _
            "<!--EndFragment--> " & vbCrLf & _
            "</BODY> " & vbCrLf & _
            "</HTML>"
    Clipboard.SetText(meh, TextDataFormat.Html)

End Sub

I also did simply something like this:-

Clipboard.SetText(TextBox1.text, TextDataFormat.Html)

Then I was pasting different strings with carefully counted bytes for StartHTML, etc...

Version:0.9
StartHTML:71
EndHTML:178
StartFragment:71
EndFragment:178
<!DOCTYPE html>
<HTML> 
<BODY> 
<B>bold.</B> <I><B>This is bold italic.</B></I> <I>This</I> 
</BODY> 
</HTML>

I was able to reproduce this unexpected behavior on VB2010, VB2017 on Windows 10 and Windows 7

Worth to add that I am using Clipdiary application which after running the code above returns something like "Can't make description and title"

I wonder could someone run this code and see what I am doing wrong because I get demanted here; I also found this code somewhere:-

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    PasteLink("http://www.experts-exchange.com/M_1539809.html", "Idle_Mind's Profile")
End Sub

Public Sub PasteLink(ByVal link As String, ByVal description As String)
    Const sContextStart As String = "<HTML><BODY><!--StartFragment -->"
    Const sContextEnd As String = "<!--EndFragment --></BODY></HTML>"
    Const m_sDescription As String = _
        "Version:1.0" & vbCrLf & _
        "StartHTML:aaaaaaaaaa" & vbCrLf & _
        "EndHTML:bbbbbbbbbb" & vbCrLf & _
        "StartFragment:cccccccccc" & vbCrLf & _
        "EndFragment:dddddddddd" & vbCrLf

    Dim sHtmlFragment As String = _
        "<A HREF=" & Chr(34) & link & Chr(34) & ">" _
        & description & "</A>"

    Dim sData As String = m_sDescription & sContextStart & sHtmlFragment & sContextEnd
    sData = sData.Replace("aaaaaaaaaa", m_sDescription.Length.ToString.PadLeft(10, "0"))
    sData = sData.Replace("bbbbbbbbbb", sData.Length.ToString.PadLeft(10, "0"))
    sData = sData.Replace("cccccccccc", (m_sDescription & sContextStart).Length.ToString.PadLeft(10, "0"))
    sData = sData.Replace("dddddddddd", (m_sDescription & sContextStart & sHtmlFragment).Length.ToString.PadLeft(10, "0"))

    Clipboard.SetDataObject(New DataObject(DataFormats.Html, sData))
End Sub

Which didn't work either, basically here Clipboard is set as DataObject , yet it does not work.

EXPLANATION EDIT:

I realized after the the guys shed different light on it that there is a different explanation of this issue.

My app was generating a Signature which was then placed into clipboard for pasting it to GMAIL.

If I use my original code here is what happens:

  1. I try to paste in Signature section on Gmail - nothing pastes

  2. I paste this inside or MSWORD - pastes OK

  3. Then I can paste this into Gmail.

So something must have changed to this window on Gmail so it doesn't work anymore and since this whole clipboard.settext module is somewhat not working properly it generated the whole misunderstanding of the issue driving me crazy.

Also it looks that the code provided by Derek works on that Gmail Signature windows inside settings even in first time then I will need to redesign my function.

You can see if this works for you. I use this pretty regularly.

It looks very similar to yours though, and it is is c# but you can use an online code translator.

   public void SetHyperlinkOnClipboard( string link, string description )
   {
      try
      {
         const string sContextStart = "<HTML><BODY><!--StartFragment -->";
         const string sContextEnd = "<!--EndFragment --></BODY></HTML>";
         const string m_sDescription = "Version:0.9" + Constants.vbCrLf + "StartHTML:aaaaaaaaaa" + Constants.vbCrLf + "EndHTML:bbbbbbbbbb" + Constants.vbCrLf + "StartFragment:cccccccccc" + Constants.vbCrLf + "EndFragment:dddddddddd" + Constants.vbCrLf;

         string sHtmlFragment = "<A HREF=" + Strings.Chr( 34 ) + link + Strings.Chr( 34 ) + ">" + description + "</A>";

         string sData = m_sDescription + sContextStart + sHtmlFragment + sContextEnd;
         sData = sData.Replace( "aaaaaaaaaa", m_sDescription.Length.ToString().PadLeft( 10, '0' ) );
         sData = sData.Replace( "bbbbbbbbbb", sData.Length.ToString().PadLeft( 10, '0' ) );
         sData = sData.Replace( "cccccccccc", ( m_sDescription + sContextStart ).Length.ToString().PadLeft( 10, '0' ) );
         sData = sData.Replace( "dddddddddd", ( m_sDescription + sContextStart + sHtmlFragment ).Length.ToString().PadLeft( 10, '0' ) );
         Clipboard.SetDataObject( new DataObject( DataFormats.Html, sData ), true );    
      }
      catch( Exception ex )
      {

      }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM